-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 给 three-utils 的 *Collection 中的函数添加状态
- Loading branch information
Showing
20 changed files
with
1,549 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,4 @@ dist | |
**/.dumi/tmp | ||
**/.dumi/tmp-production | ||
|
||
/apps/study-webgl | ||
/playground/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./util/LoaderUtil" | ||
export * from "./widget/CustomGridHelper" | ||
export * from "./widget/threeHelper/ThreeHelper" | ||
export * from "./widget/threeHelper/plugins" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
import * as THREE from "three" | ||
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader" | ||
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js" | ||
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader.js" | ||
|
||
const textureLoader = new THREE.TextureLoader() | ||
const cubeTextureLoader = new THREE.CubeTextureLoader() | ||
const rgbeLoader = new RGBELoader() | ||
const dracoLoader = new DRACOLoader() | ||
const gltfLoader = new GLTFLoader() | ||
|
||
dracoLoader.setDecoderPath("/draco/") | ||
gltfLoader.setDRACOLoader(dracoLoader) | ||
|
||
export const LoaderUtil = { | ||
gltfLoader: new GLTFLoader(), | ||
textureLoader: new THREE.TextureLoader(), | ||
cubeTextureLoader: new THREE.CubeTextureLoader(), | ||
rgbeLoader: new RGBELoader(), | ||
textureLoader, | ||
cubeTextureLoader, | ||
rgbeLoader, | ||
dracoLoader, | ||
gltfLoader, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { genInfo } from "@zxtool/utils/dist/util" | ||
|
||
export const genZTUInfo = genInfo("@zxtool/three-utils") |
54 changes: 54 additions & 0 deletions
54
packages/zxtool-three-utils/src/widget/CustomGridHelper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as THREE from "three" | ||
|
||
export interface CustomGridHelperProps { | ||
xSize?: number | ||
xCount?: number | ||
ySize?: number | ||
yCount?: number | ||
lineColor?: THREE.ColorRepresentation | ||
centerLineColor?: THREE.ColorRepresentation | ||
} | ||
|
||
export class CustomGridHelper extends THREE.LineSegments { | ||
type = "CustomGridHelper" | ||
|
||
constructor(props: CustomGridHelperProps = {}) { | ||
const { xSize = 1, xCount = 10, ySize = 1, yCount = 10, lineColor = 0x888888, centerLineColor = 0x444444 } = props | ||
|
||
const lineColorArray = new THREE.Color(lineColor).toArray() | ||
const centerLineColorArray = new THREE.Color(centerLineColor).toArray() | ||
|
||
const xHalf = (xCount * xSize) / 2 | ||
const yHalf = (yCount * ySize) / 2 | ||
|
||
const vertices: number[] = [] | ||
const colors: number[] = [] | ||
|
||
for (let x = -xHalf; x <= xHalf; x += xSize) { | ||
vertices.push(x, 0, -yHalf, x, 0, yHalf) | ||
const colorArr = x === 0 ? centerLineColorArray : lineColorArray | ||
colors.push(...colorArr, ...colorArr) | ||
} | ||
for (let y = -yHalf; y <= yHalf; y += ySize) { | ||
vertices.push(-xHalf, 0, y, xHalf, 0, y) | ||
const colorArr = y === 0 ? centerLineColorArray : lineColorArray | ||
colors.push(...colorArr, ...colorArr) | ||
} | ||
|
||
const geometry = new THREE.BufferGeometry() | ||
geometry.setAttribute("position", new THREE.Float32BufferAttribute(vertices, 3)) | ||
geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3)) | ||
|
||
const material = new THREE.LineBasicMaterial({ vertexColors: true, toneMapped: false }) | ||
|
||
super(geometry, material) | ||
} | ||
|
||
dispose() { | ||
this.geometry.dispose() | ||
// @ts-ignore | ||
this.material.dispose() | ||
} | ||
} | ||
|
||
new CustomGridHelper({}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/zxtool-three-utils/src/widget/threeHelper/plugins/CameraPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as THREE from "three" | ||
import { ThreeHelperPlugin, ThreeHelperPluginProps } from "." | ||
import { genZTUInfo } from "../../../util/util" | ||
|
||
const genInfo = genZTUInfo("CameraPlugin") | ||
|
||
export class CameraPlugin implements ThreeHelperPlugin { | ||
private key = Symbol.for("animation") | ||
private _camera: THREE.Camera | ||
private addProps!: ThreeHelperPluginProps | ||
|
||
get camera() { | ||
return this.camera | ||
} | ||
set camera(c: THREE.Camera) { | ||
if (!this.addProps) throw new Error(genInfo("设置 camera 之前需要先添加此插件")) | ||
const { widgetCollection, emitter } = this.addProps | ||
|
||
this._camera = c | ||
widgetCollection.set("camera", c) | ||
emitter.emit("camera", c) | ||
} | ||
|
||
constructor() { | ||
this._camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1e4) | ||
this._camera.position.set(5, 5, 5) | ||
} | ||
|
||
add(props: ThreeHelperPluginProps): CameraPlugin { | ||
this.addProps = props | ||
const { widgetCollection, emitter, threeHelper } = props | ||
|
||
const plugin = threeHelper.pluginCollection.get(this.key) as CameraPlugin | ||
if (plugin) { | ||
console.error(genInfo("已经存在一个 CameraPlugin, 不能重复添加")) | ||
return plugin | ||
} | ||
|
||
widgetCollection.set("camera", this.camera) | ||
emitter.emit("camera", this.camera) | ||
|
||
return this | ||
} | ||
|
||
remove(): void { | ||
if (!this.addProps) throw new Error(genInfo("未添加的插件不能被移除")) | ||
|
||
const { widgetCollection, emitter } = this.addProps | ||
|
||
widgetCollection.delete("camera") | ||
emitter.clearHistory("camera") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.