From f86a3f9c99a2da2becd7c13e0d31783291072c71 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Mon, 11 Nov 2024 21:53:51 -0500 Subject: [PATCH] Remove examples --- .../examples/misc_animation_keys.ts | 132 ------ .../examples/webgl_buffergeometry.ts | 178 -------- examples-testing/examples/webgl_decals.ts | 240 ---------- .../examples/webgl_geometry_extrude_shapes.ts | 149 ------ .../examples/webgl_geometry_shapes.ts | 384 ---------------- .../examples/webgl_geometry_teapot.ts | 188 -------- .../examples/webgl_gpgpu_birds.ts | 313 ------------- .../examples/webgl_gpgpu_birds_gltf.ts | 428 ------------------ .../examples/webgl_instancing_morph.ts | 153 ------- .../examples/webgl_instancing_performance.ts | 266 ----------- .../examples/webgl_instancing_raycast.ts | 120 ----- .../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 | 165 ------- examples-testing/examples/webgl_layers.ts | 125 ----- examples-testing/examples/webgl_lensflares.ts | 137 ------ examples-testing/examples/webgl_lightprobe.ts | 145 ------ .../examples/webgl_lines_colors.ts | 181 -------- .../examples/webgl_lines_dashed.ts | 186 -------- examples-testing/examples/webgl_lines_fat.ts | 250 ---------- .../examples/webgl_lines_fat_raycasting.ts | 289 ------------ examples-testing/examples/webgl_loader_3dm.ts | 95 ---- examples-testing/examples/webgl_loader_3ds.ts | 62 --- examples-testing/examples/webgl_loader_3mf.ts | 115 ----- .../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 | 177 -------- 34 files changed, 5945 deletions(-) delete mode 100644 examples-testing/examples/misc_animation_keys.ts delete mode 100644 examples-testing/examples/webgl_buffergeometry.ts delete mode 100644 examples-testing/examples/webgl_decals.ts delete mode 100644 examples-testing/examples/webgl_geometry_extrude_shapes.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_gpgpu_birds.ts delete mode 100644 examples-testing/examples/webgl_gpgpu_birds_gltf.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_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_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_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 diff --git a/examples-testing/examples/misc_animation_keys.ts b/examples-testing/examples/misc_animation_keys.ts deleted file mode 100644 index a2e43b586..000000000 --- a/examples-testing/examples/misc_animation_keys.ts +++ /dev/null @@ -1,132 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let stats: Stats, clock: THREE.Clock; -let scene: THREE.Scene, camera: THREE.PerspectiveCamera, renderer: THREE.WebGLRenderer, mixer: THREE.AnimationMixer; - -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/webgl_buffergeometry.ts b/examples-testing/examples/webgl_buffergeometry.ts deleted file mode 100644 index 6a802ab6c..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: HTMLElement, stats: Stats; - -let camera: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; - -let mesh: THREE.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: THREE.BufferAttribute) { - this.array = null as unknown as THREE.TypedArray; - } - - 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_decals.ts b/examples-testing/examples/webgl_decals.ts deleted file mode 100644 index acc5b2193..000000000 --- a/examples-testing/examples/webgl_decals.ts +++ /dev/null @@ -1,240 +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: THREE.WebGLRenderer, scene: THREE.Scene, camera: THREE.PerspectiveCamera, stats: Stats; -let mesh: THREE.Mesh; -let raycaster: THREE.Raycaster; -let line: THREE.Line; - -const intersection = { - intersects: false, - point: new THREE.Vector3(), - normal: new THREE.Vector3(), -}; -const mouse = new THREE.Vector2(); -const intersects: THREE.Intersection[] = []; - -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: THREE.Mesh[] = []; -let mouseHelper: THREE.Mesh; -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: PointerEvent) { - if (event.isPrimary) { - checkIntersection(event.clientX, event.clientY); - } - } - - function checkIntersection(x: number, y: number) { - 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 normalMatrix = new THREE.Matrix3().getNormalMatrix(mesh.matrixWorld); - - const n = intersects[0].face!.normal.clone(); - n.applyNormalMatrix(normalMatrix); - 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] as THREE.Mesh; - mesh.material = new THREE.MeshPhongMaterial({ - specular: 0x111111, - map: map, - specularMap: specularMap, - normalMap: normalMap, - shininess: 25, - }); - - scene.add(mesh); - mesh.scale.multiplyScalar(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); - - mesh.attach(m); -} - -function removeDecals() { - decals.forEach(function (d) { - mesh.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_geometry_extrude_shapes.ts b/examples-testing/examples/webgl_geometry_extrude_shapes.ts deleted file mode 100644 index 386e8d574..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: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer, controls: TrackballControls; - -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 as any).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_shapes.ts b/examples-testing/examples/webgl_geometry_shapes.ts deleted file mode 100644 index 2042c3530..000000000 --- a/examples-testing/examples/webgl_geometry_shapes.ts +++ /dev/null @@ -1,384 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container: HTMLDivElement, stats: Stats; - -let camera: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; - -let group: THREE.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: THREE.Shape, - extrudeSettings: THREE.ExtrudeGeometryOptions, - color: number, - x: number, - y: number, - z: number, - rx: number, - ry: number, - rz: number, - s: number, - ) { - // flat shape with texture - // note: default UVs generated by THREE.ShapeGeometry are simply the x- and y-coordinates of the vertices - - let geometry: THREE.BufferGeometry = 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: THREE.Path, - color: number, - x: number, - y: number, - z: number, - rx: number, - ry: number, - rz: number, - s: number, - ) { - // 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: PointerEvent) { - if (event.isPrimary === false) return; - - pointerXOnPointerDown = event.clientX - windowHalfX; - targetRotationOnPointerDown = targetRotation; - - document.addEventListener('pointermove', onPointerMove); - document.addEventListener('pointerup', onPointerUp); -} - -function onPointerMove(event: PointerEvent) { - if (event.isPrimary === false) return; - - pointerX = event.clientX - windowHalfX; - - targetRotation = targetRotationOnPointerDown + (pointerX - pointerXOnPointerDown) * 0.02; -} - -function onPointerUp(event: PointerEvent) { - 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 b94b16db4..000000000 --- a/examples-testing/examples/webgl_geometry_teapot.ts +++ /dev/null @@ -1,188 +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: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; -let cameraControls; -let effectController: { - newTess: number; - bottom: boolean; - lid: boolean; - body: boolean; - fitLid: boolean; - nonblinn: boolean; - newShading: string; -}; -const teapotSize = 300; -let ambientLight: THREE.AmbientLight, light: THREE.DirectionalLight; - -let tess = -1; // force initialization -let bBottom: boolean; -let bLid: boolean; -let bBody: boolean; -let bFitLid: boolean; -let bNonBlinn: boolean; -let shading: string; - -let teapot: THREE.Mesh, textureCube: THREE.CubeTexture; -const materials: Record = {}; - -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_gpgpu_birds.ts b/examples-testing/examples/webgl_gpgpu_birds.ts deleted file mode 100644 index 5bb4ed25b..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, Variable } 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(...args: number[]) { - for (let i = 0; i < args.length; i++) { - vertices.array[v++] = args[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: HTMLDivElement, stats: Stats; -let camera: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; -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: GPUComputationRenderer; -let velocityVariable: Variable; -let positionVariable: Variable; -let positionUniforms: Record; -let velocityUniforms: Record; -let birdUniforms: Record; - -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: THREE.DataTexture) { - 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: THREE.DataTexture) { - 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: PointerEvent) { - 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 df84fb256..000000000 --- a/examples-testing/examples/webgl_gpgpu_birds_gltf.ts +++ /dev/null @@ -1,428 +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, Variable } 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: THREE.DataTexture, - durationAnimation: number, - birdMesh: THREE.Mesh, - materialShader: THREE.WebGLProgramParametersWithUniforms, - indicesPerBird: number; - -function nextPowerOf2(n: number) { - return Math.pow(2, Math.ceil(Math.log(n) / Math.log(2))); -} - -function lerp(value1: number, value2: number, amount: number) { - 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] as THREE.Mesh).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] = 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] = 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] = 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: HTMLDivElement, stats: Stats; -let camera: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; -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: GPUComputationRenderer; -let velocityVariable: Variable; -let positionVariable: Variable; -let positionUniforms: Record; -let velocityUniforms: Record; - -interface EffectController { - separation: number; - alignment: number; - cohesion: number; - freedom: number; - size: number; - count: number; -} - -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: 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: THREE.DataTexture) { - 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: THREE.DataTexture) { - 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: PointerEvent) { - 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_instancing_morph.ts b/examples-testing/examples/webgl_instancing_morph.ts deleted file mode 100644 index fb76abe35..000000000 --- a/examples-testing/examples/webgl_instancing_morph.ts +++ /dev/null @@ -1,153 +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: THREE.PerspectiveCamera, - scene: THREE.Scene, - renderer: THREE.WebGLRenderer, - stats: Stats, - mesh: THREE.InstancedMesh, - mixer: THREE.AnimationMixer, - dummy: THREE.Mesh; - -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] as THREE.Mesh; - - 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 705d2cf36..000000000 --- a/examples-testing/examples/webgl_instancing_performance.ts +++ /dev/null @@ -1,266 +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: HTMLElement, stats: Stats, gui: GUI, guiStatsEl: HTMLDivElement; -let camera: THREE.PerspectiveCamera, - controls: OrbitControls, - scene: THREE.Scene, - renderer: THREE.WebGLRenderer, - material: THREE.MeshNormalMaterial; - -// gui - -const Method = { - INSTANCED: 'INSTANCED', - MERGED: 'MERGED', - NAIVE: 'NAIVE', -}; - -const api = { - method: Method.INSTANCED, - count: 1000, -}; - -// - -init(); -initMesh(); - -// - -function clean() { - const meshes: THREE.Mesh[] = []; - - scene.traverse(function (object) { - if ((object as THREE.Mesh).isMesh) meshes.push(object as THREE.Mesh); - }); - - for (let i = 0; i < meshes.length; i++) { - const mesh = meshes[i]; - (mesh.material as THREE.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: THREE.Matrix4) { - 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: THREE.BufferGeometry) { - 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: THREE.BufferGeometry) { - 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: THREE.BufferGeometry) { - 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: THREE.BufferGeometry) { - 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: number, decimals: number) { - 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 861bc9c51..000000000 --- a/examples-testing/examples/webgl_instancing_raycast.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'; -import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; - -let camera: THREE.PerspectiveCamera, - scene: THREE.Scene, - renderer: THREE.WebGLRenderer, - controls: OrbitControls, - stats: Stats; - -let mesh: THREE.InstancedMesh; -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: MouseEvent) { - 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 d0904e8e4..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: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer, stats: Stats; - -const api = { - count: 2000, - distribution: 'random', - resample: resample, - surfaceColor: 0xfff784, - backgroundColor: 0xe39469, -}; - -let stemMesh: THREE.InstancedMesh, blossomMesh: THREE.InstancedMesh; -let stemGeometry: THREE.BufferGeometry, blossomGeometry: THREE.BufferGeometry; -let stemMaterial: THREE.Material | THREE.Material[], blossomMaterial: THREE.Material | THREE.Material[]; - -let sampler: MeshSurfaceSampler; -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: number) { - 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: number) { - 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') as THREE.Mesh; - const _blossomMesh = gltf.scene.getObjectByName('Blossom') as THREE.Mesh; - - 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: number) { - 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: number) { - // 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 c6aca942f..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: HTMLElement, stats: Stats; - -let camera: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; - -let raycaster: THREE.Raycaster, pointer: THREE.Vector2; - -let mesh: THREE.Mesh, line: THREE.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: THREE.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: PointerEvent) { - 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 as THREE.BufferAttribute; - const meshPosition = mesh.geometry.attributes.position as THREE.BufferAttribute; - - 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 d26cee370..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: Stats; -let camera: THREE.PerspectiveCamera, scene: THREE.Scene, raycaster: THREE.Raycaster, renderer: THREE.WebGLRenderer; - -let INTERSECTED: (THREE.Mesh & { currentHex?: number }) | null; -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: MouseEvent) { - 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 as THREE.Mesh; - 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 d48fa232d..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: HTMLElement, stats: Stats; -let camera: THREE.PerspectiveCamera, controls: TrackballControls, scene: THREE.Scene, renderer: THREE.WebGLRenderer; -let pickingTexture: THREE.WebGLRenderTarget, pickingScene: THREE.Scene; -let highlightBox: THREE.Mesh; - -const pickingData: { position: THREE.Vector3; rotation: THREE.Euler; scale: THREE.Vector3 }[] = []; - -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: THREE.BoxGeometry, id: number) { - 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: THREE.BoxGeometry, color: THREE.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: PointerEvent) { - 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 4b083ffe9..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: Stats; -let camera: THREE.OrthographicCamera, scene: THREE.Scene, raycaster: THREE.Raycaster, renderer: THREE.WebGLRenderer; - -let theta = 0; -let INTERSECTED: (THREE.Mesh & { currentHex?: number }) | null; - -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: PointerEvent) { - 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 as THREE.Mesh; - 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 891e5541c..000000000 --- a/examples-testing/examples/webgl_interactive_lines.ts +++ /dev/null @@ -1,165 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'three/addons/libs/stats.module.js'; - -let container: HTMLElement, stats: Stats; -let camera: THREE.PerspectiveCamera, - scene: THREE.Scene, - raycaster: THREE.Raycaster, - renderer: THREE.WebGLRenderer, - parentTransform: THREE.Object3D, - sphereInter: THREE.Mesh; - -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: PointerEvent) { - 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_layers.ts b/examples-testing/examples/webgl_layers.ts deleted file mode 100644 index f24cdc8b9..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: HTMLDivElement, stats: Stats; -let camera: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; - -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 56651e282..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: HTMLDivElement, stats: Stats; - -let camera: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; -let controls: FlyControls; - -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: number, s: number, l: number, x: number, y: number, z: number) { - 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 1427cce47..000000000 --- a/examples-testing/examples/webgl_lightprobe.ts +++ /dev/null @@ -1,145 +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'; - -import { LightProbeHelper } from 'three/addons/helpers/LightProbeHelper.js'; - -let mesh: THREE.Mesh, - renderer: THREE.WebGLRenderer, - scene: THREE.Scene, - camera: THREE.PerspectiveCamera; - -let gui; - -let lightProbe: THREE.LightProbe; -let directionalLight: THREE.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: string, postfix: string) { - 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)); - lightProbe.intensity = API.lightProbeIntensity; - lightProbe.position.set(-10, 0, 0); // position not used in scene lighting calculations (helper honors the position, however) - - 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); - - // helper - const helper = new LightProbeHelper(lightProbe, 1); - scene.add(helper); - - 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_lines_colors.ts b/examples-testing/examples/webgl_lines_colors.ts deleted file mode 100644 index 68bc979b0..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: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; - -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: [THREE.Material, number, [number, number, number], THREE.BufferGeometry][] = [ - [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: PointerEvent) { - 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 as THREE.Line).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 4bddc4848..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: THREE.WebGLRenderer, scene: THREE.Scene, camera: THREE.PerspectiveCamera, stats: Stats; -const objects: THREE.Object3D[] = []; - -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: number, height: number, depth: number) { - (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 as THREE.Line).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 ba63c7224..000000000 --- a/examples-testing/examples/webgl_lines_fat.ts +++ /dev/null @@ -1,250 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'stats-gl'; - -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: Line2, - renderer: THREE.WebGLRenderer, - scene: THREE.Scene, - camera: THREE.PerspectiveCamera, - camera2: THREE.PerspectiveCamera, - controls: OrbitControls; -let line1: THREE.Line; -let matLine: LineMaterial, matLineBasic: THREE.LineBasicMaterial, matLineDashed: THREE.LineDashedMaterial; -let stats: Stats; -let gui: GUI; - -// viewport -let insetWidth: number; -let insetHeight: number; - -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({ horizontal: false }); - stats.init(renderer); - 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); - - controls.update(); - - 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, - '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 fb622394e..000000000 --- a/examples-testing/examples/webgl_lines_fat_raycasting.ts +++ /dev/null @@ -1,289 +0,0 @@ -import * as THREE from 'three'; - -import Stats from 'stats-gl'; - -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: Line2, thresholdLine: Line2, segments: LineSegments2, thresholdSegments: LineSegments2; -let renderer: THREE.WebGLRenderer, scene: THREE.Scene, camera: THREE.PerspectiveCamera, controls: OrbitControls; -let sphereInter: THREE.Mesh, - sphereOnLine: THREE.Mesh; -let stats: Stats; -let gui: GUI; -let clock: THREE.Clock; - -const color = new THREE.Color(); - -const pointer = new THREE.Vector2(Infinity, Infinity); - -const raycaster = new THREE.Raycaster(); - -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({ horizontal: false }); - stats.init(renderer); - document.body.appendChild(stats.dom); - - initGui(); -} - -function onWindowResize() { - camera.aspect = window.innerWidth / window.innerHeight; - camera.updateProjectionMatrix(); - - renderer.setSize(window.innerWidth, window.innerHeight); -} - -function onPointerMove(event: PointerEvent) { - 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 = ''; - } - - renderer.render(scene, camera); - - stats.update(); -} - -// - -function switchLine(val: number) { - 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_loader_3dm.ts b/examples-testing/examples/webgl_loader_3dm.ts deleted file mode 100644 index a363c2132..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: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; -let controls: OrbitControls, gui: 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: { name: string; visible: boolean }[]) { - 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 ac3a6e232..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: HTMLDivElement, controls: TrackballControls; -let camera: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; - -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 as THREE.Mesh).isMesh) { - (child as THREE.Mesh).material.specular.setScalar(0.1); - (child as THREE.Mesh).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 eecda516b..000000000 --- a/examples-testing/examples/webgl_loader_3mf.ts +++ /dev/null @@ -1,115 +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: THREE.PerspectiveCamera, - scene: THREE.Scene, - renderer: THREE.WebGLRenderer, - object: THREE.Group, - loader: ThreeMFLoader, - controls: OrbitControls; - -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: string) { - loader.load('models/3mf/' + asset + '.3mf', function (group) { - if (object) { - object.traverse(function (child) { - if ((child as THREE.Mesh).material) - (child as THREE.Mesh).material.dispose(); - if ( - (child as THREE.Mesh).material && - (child as THREE.Mesh).material.map - ) - (child as THREE.Mesh).material.map!.dispose(); - if ((child as THREE.Mesh).geometry) (child as THREE.Mesh).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 3cfb49b44..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: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; - -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 7569a0448..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: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; - -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 cfdf0bc7f..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: THREE.PerspectiveCamera, controls: OrbitControls, scene: THREE.Scene, renderer: THREE.WebGLRenderer; -let mixer: THREE.AnimationMixer; - -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 e1e827714..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: HTMLElement, stats: Stats, clock: THREE.Clock; -let camera: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer, elf: THREE.Scene; - -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 7b2b57bd5..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: HTMLElement, stats: Stats, clock: THREE.Clock, controls: OrbitControls; -let camera: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer, mixer: THREE.AnimationMixer; - -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 69bf22b48..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: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGLRenderer; - -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 af4de6ac4..000000000 --- a/examples-testing/examples/webgl_loader_fbx.ts +++ /dev/null @@ -1,177 +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: THREE.PerspectiveCamera, - scene: THREE.Scene, - renderer: THREE.WebGLRenderer, - stats: Stats, - object: THREE.Group, - loader: FBXLoader, - guiMorphsFolder: GUI; -let mixer: THREE.AnimationMixer | null; - -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: string) { - loader.load('models/fbx/' + asset + '.fbx', function (group) { - if (object) { - object.traverse(function (child) { - if ((child as THREE.Mesh).material) { - const materials = Array.isArray((child as THREE.Mesh).material) - ? (child as THREE.Mesh).material - : [(child as THREE.Mesh).material]; - materials.forEach(material => { - if ((material as THREE.MeshPhongMaterial).map) - (material as THREE.MeshPhongMaterial).map!.dispose(); - (material as THREE.MeshPhongMaterial).dispose(); - }); - } - - if ((child as THREE.Mesh).geometry) (child as THREE.Mesh).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 as THREE.Mesh).isMesh) { - child.castShadow = true; - child.receiveShadow = true; - - if ((child as THREE.Mesh).morphTargetDictionary) { - guiMorphsFolder.show(); - const meshFolder = guiMorphsFolder.addFolder(child.name || child.uuid); - Object.keys((child as THREE.Mesh).morphTargetDictionary!).forEach(key => { - meshFolder.add( - (child as THREE.Mesh).morphTargetInfluences!, - (child as THREE.Mesh).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(); -}