repo_name
string
dataset
string
owner
string
lang
string
func_name
string
code
string
docstring
string
url
string
sha
string
effects-runtime
github_2023
galacean
typescript
broadcastPlayerEvent
function broadcastPlayerEvent (player: Player, isCreate: boolean) { Object.keys(pluginLoaderMap).forEach(key => { const ctrl = pluginLoaderMap[key]; const func = isCreate ? ctrl.onPlayerCreated : ctrl.onPlayerDestroy; func?.(player); }); }
/** * 播放器在实例化、销毁(`dispose`)时分别触发插件的 `onPlayerCreated`、`onPlayerDestroy` 回调 * @param player - 播放器 * @param isCreate - 是否处于实例化时 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/packages/effects/src/player.ts#L997-L1004
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
assertNoConcurrentPlayers
function assertNoConcurrentPlayers () { const runningPlayers = []; for (const player of playerMap.values()) { if (!player.paused) { runningPlayers.push(player); } } if (runningPlayers.length > 1) { logger.error(`Current running player count: ${runningPlayers.length}, see ${HELP_LINK['Current running player count']}.`, runningPlayers); } }
/** * 同时允许的播放器数量超过 1 时打印错误 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/packages/effects/src/player.ts#L1009-L1021
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
assertContainer
function assertContainer (container?: HTMLElement | null): asserts container is HTMLElement { if (container === undefined || container === null) { throw new Error(`Container is not an HTMLElement, see ${HELP_LINK['Container is not an HTMLElement']}.`); } }
/** * 创建播放器传入的容器不是 `HTMLElement` 时抛出错误 * @param container */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/packages/effects/src/player.ts#L1027-L1031
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
DeviceProxy.setSystemInfo
setSystemInfo (systemInfo: SystemInfo) { const { performance, platform, model = 'UNKNOWN_DEVICE', system = 'Unknown', } = systemInfo; this.isIOS = platform === 'iOS'; this.model = model; this.system = system; this.setLevel(performance); }
/** * 设置 JSAPI 返回的系统信息 * @param systemInfo - JSAPI 返回的系统信息 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/alipay-downgrade/src/device-proxy.ts#L40-L51
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
DeviceProxy.getDowngradeDecision
getDowngradeDecision (result: any): DowngradeDecision { let resultType = undefined; let resultReason = undefined; if (result.error) { // 无权调用的情况下不降级 return { downgrade: result.error !== 4, level: this.getRenderLevel(), reason: 'api error: ' + result.error, }; } try { const ret = isString(result) ? JSON.parse(result) : result; if ('downgradeResultType' in ret) { resultType = ret.downgradeResultType; } else if ('resultType' in ret) { resultType = ret.resultType; resultReason = ret.resultReason; } if (result.context) { const deviceInfo = result.context.deviceInfo; if (deviceInfo) { const { deviceLevel } = deviceInfo; const newLevel = getDeviceLevel(deviceLevel); if (newLevel !== DeviceLevel.Unknown) { this.level = newLevel; } } } } catch (ex: any) { logger.error(ex); } if (resultType === undefined) { return { downgrade: true, level: this.getRenderLevel(), reason: 'call downgrade fail', }; } if (resultType === 1) { return { downgrade: true, level: this.getRenderLevel(), reason: getDowngradeReason(resultReason), }; } if (isAlipayMiniApp() && this.downgradeForMiniprogram()) { return { downgrade: true, level: this.getRenderLevel(), reason: 'Force downgrade by downgrade plugin', }; } return { downgrade: false, level: this.getRenderLevel(), reason: `${resultType}`, }; }
/** * 根据传入的 JSAPI 降级结果,返回设备的降级决定 * * @param result - JSAPI 返回的降级结果 * @returns 设备降级决定 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/alipay-downgrade/src/device-proxy.ts#L59-L125
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
DeviceProxy.getRenderLevel
getRenderLevel (): SceneRenderLevel { if (this.level === DeviceLevel.High) { return spec.RenderLevel.S; } else if (this.level === DeviceLevel.Medium) { return spec.RenderLevel.A; } else if (this.level === DeviceLevel.Low) { return spec.RenderLevel.B; } else { return this.isIOS ? spec.RenderLevel.S : spec.RenderLevel.B; } }
/** * 获取设备渲染等级 * @returns 设备渲染等级 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/alipay-downgrade/src/device-proxy.ts#L131-L141
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
DowngradeJudge.getDowngradeResult
getDowngradeResult (): DowngradeResult { const { downgradeCallback } = this.options; if (downgradeCallback) { const result = downgradeCallback(this.device); if (result) { if (!result.reason) { result.reason = 'downgradeCallback'; } return result; } } this.isIOS = this.device.platform === 'iOS'; this.level = this.getRenderLevel(); if (this.device.model) { const deviceModel = this.device.model.toLowerCase(); const modelList = this.isIOS ? downgradeModels.iPhone : downgradeModels.android; const findModel = modelList.find(m => { const testModel = m.toLowerCase(); if (this.isIOS) { return testModel === deviceModel; } else { return testModel.includes(deviceModel) || deviceModel.includes(testModel); } }); if (findModel !== undefined) { return { downgrade: true, level: this.level, reason: 'Downgrade by model list', deviceInfo: this.device, }; } } const osVersionList = this.isIOS ? downgradeVersions.iOS : downgradeVersions.android; const findOS = osVersionList.find(v => v === this.device.osVersion); if (findOS !== undefined) { return { downgrade: true, level: this.level, reason: 'Downgrade by OS version list', deviceInfo: this.device, }; } return { downgrade: false, level: this.level, reason: '', deviceInfo: this.device, }; }
/** * 根据输入的设备信息和降级选项,以及内置的硬件机型和系统版本降级列表 * 返回当前设备降级相关的结果 * * @returns 降级结果 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/downgrade/src/downgrade-judge.ts#L25-L84
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
GizmoComponent.getDefaultRenderMode
getDefaultRenderMode (): GeometryDrawMode { let result: GeometryDrawMode; const gizmoSubType = this.subType; switch (gizmoSubType) { case GizmoSubType.particleEmitter: case GizmoSubType.modelWireframe: case GizmoSubType.frustum: case GizmoSubType.directionLight: case GizmoSubType.spotLight: case GizmoSubType.pointLight: case GizmoSubType.floorGrid: result = constants.LINES; break; default: result = constants.TRIANGLES; break; } return result; }
/** * 获取默认绘制模式 * @returns 绘制模式常量 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/gizmo-component.ts#L292-L314
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
GizmoComponent.getDefaultSize
getDefaultSize () { let result = {}; const gizmoSubType = this.subType; switch (gizmoSubType) { case GizmoSubType.box: result = { width: 1, height: 1, depth: 1 }; break; case GizmoSubType.sphere: result = { radius: 1 }; break; case GizmoSubType.cylinder: result = { radius: 1, height: 1 }; break; case GizmoSubType.cone: result = { radius: 1, height: 1 }; break; default: result = {}; break; } return result; }
/** * 获取默认尺寸 * @returns 几何体尺寸 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/gizmo-component.ts#L320-L348
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
GizmoComponent.createBoundingBoxContent
createBoundingBoxContent (item: VFXItem, meshesToAdd: Mesh[]) { const gizmoItem = this.item; const shape = { shape: 'Box', width: this.size.width, height: this.size.height, depth: this.size.depth, center: this.size.center, }; const engine = gizmoItem.composition?.renderer.engine; assertExist(engine); this.targetItem = item; if (shape) { const mesh = gizmoItem._content = createMeshFromShape(engine, shape, { color: this.color, depthTest: this.depthTest }); meshesToAdd.push(mesh); } }
/** * 创建 BoundingBox 几何体模型 * @param item - VFXItem * @param meshesToAdd - 插件缓存的 Mesh 数组 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/gizmo-component.ts#L395-L414
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
GizmoComponent.createBasicContent
createBasicContent (item: VFXItem, meshesToAdd: Mesh[], subType: GizmoSubType, iconTextures?: Map<string, Texture>) { const gizmoItem = this.item; const options = { size: this.size, color: this.color, renderMode: this.renderMode, depthTest: this.depthTest, }; const engine = gizmoItem.composition?.renderer.engine; assertExist(engine); const mesh = gizmoItem._content = createMeshFromSubType(engine, subType, this.boundingMap, iconTextures, options) as Mesh; this.targetItem = item; meshesToAdd.push(mesh); }
/** * 创建基础几何体模型 * @param item - VFXItem * @param meshesToAdd - 插件缓存的 Mesh 数组 * @param subType - GizmoSubType 类型 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/gizmo-component.ts#L422-L438
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
GizmoComponent.createCombinationContent
createCombinationContent (item: VFXItem, meshesToAdd: Mesh[], subType: GizmoSubType, iconTextures?: Map<string, Texture>) { const gizmoItem = this.item; const options = { size: this.size, renderMode: this.renderMode, }; const engine = gizmoItem.composition?.renderer.engine; assertExist(engine); this.contents = createMeshFromSubType(engine, subType, this.boundingMap, iconTextures, options) as Map<Mesh, Transform>; for (const mesh of this.contents.keys()) { meshesToAdd.push(mesh); } this.targetItem = item; }
/** * 创建组合几何体模型 * @param item - VFXItem * @param meshesToAdd - 插件缓存的 Mesh 数组 * @param subType - GizmoSubType 类型 * @param iconTextures - XYZ 图标纹理(viewHelper 专用) */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/gizmo-component.ts#L447-L463
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
GizmoComponent.getHitTestParams
private getHitTestParams = (): void | HitTestCustomParams => { const item = this.item; const boundingMap = this.boundingMap; if (boundingMap.size > 0) { const boundingKeys = boundingMap.keys(); let worldMat4: Matrix4; worldMat4 = this.transform.getWorldMatrix(); const targetItem = this.targetItem; if (targetItem) { //const targetTransform = this.targetItem.transform.clone(); const worldPos = new Vector3(); const worldQuat = new Quaternion(); const worldSca = new Vector3(1, 1, 1); const gizmoSubType = this.subType; // Scale Gizmo 没有世界空间 if (this.coordinateSpace == CoordinateSpace.Local || gizmoSubType === GizmoSubType.scale) { this.targetItem.transform.assignWorldTRS(worldPos, worldQuat); } else { this.targetItem.transform.assignWorldTRS(worldPos); } const targetTransform = new Transform({ position: worldPos, quat: worldQuat, scale: worldSca, valid: true, }); // 移动\旋转\缩放gizmo去除近大远小 if ( gizmoSubType === GizmoSubType.rotation || gizmoSubType === GizmoSubType.scale || gizmoSubType === GizmoSubType.translation || gizmoSubType === GizmoSubType.camera || gizmoSubType === GizmoSubType.light ) { const camera = item.composition!.camera; const cameraPos = camera.position || [0, 0, 0]; const itemPos = targetTransform.position; const newPos = moveToPointWidthFixDistance(cameraPos, itemPos); targetTransform.setPosition(newPos.x, newPos.y, newPos.z); worldMat4 = targetTransform.getWorldMatrix(); } else { worldMat4 = targetTransform.getWorldMatrix(); } } // eslint-disable-next-line @typescript-eslint/no-this-alias const self = item; return { type: HitTestType.custom, /** * 自定义射线检测算法 * @param ray 射线参数 * @param pointInCanvas 屏幕坐标 * @returns */ collect (ray: Ray, pointInCanvas: Vector2): Vector3[] | void { const hitPositions = []; const gizmoComponent = self.getComponent(GizmoComponent); gizmoComponent.hitBounding = undefined; for (const key of boundingKeys) { const bounding = boundingMap.get(key); const center = new Vector3(); const worldCenter = new Vector3(); if (bounding?.type === BoundingType.box || bounding?.type === BoundingType.sphere) { if (bounding?.center) { center.copyFrom(bounding?.center); } worldMat4.projectPoint(center, worldCenter); } let position: Vector3 | undefined; switch (bounding?.type) { case BoundingType.box: // 立方体包围盒 { const boxHalfSize = bounding.size.clone().multiply(0.5); const boxMax = center.clone().add(boxHalfSize); const boxMin = center.clone().subtract(boxHalfSize); position = ray.intersectBox({ min: boxMin, max: boxMax }, new Vector3()); } break; case BoundingType.sphere: // 球体包围盒 // viewHelper 使用了正交投影,使用屏幕投影点计算 if (gizmoComponent.subType === GizmoSubType.viewHelper) { // 计算正交投影矩阵 const viewMat4 = self.composition!.camera.getViewMatrix(); const fov = 30; // 指定fov角度 const screenWidth = self.composition!.renderer.getWidth(); const screenHeight = self.composition!.renderer.getHeight(); const distance = 16; // 指定物体与相机的距离 const width = 2 * Math.tan(fov * Math.PI / 360) * distance; const aspect = screenWidth / screenHeight; const height = width / aspect; const proMat4 = computeOrthographicOffCenter(-width / 2, width / 2, -height / 2, height / 2, -distance, distance); // 将包围盒挂到相机的 transform 上 const cameraModelMat4 = self.composition!.camera.getInverseViewMatrix(); const localTransform = new Transform({ valid: true, }); localTransform.cloneFromMatrix(worldMat4); const pos = localTransform.position; const padding = gizmoComponent.size.padding || 1; // 指定viewHelper与屏幕右上角的边距 localTransform.setPosition((pos.x + width / 2) - padding, (pos.y + height / 2) - padding, pos.z); const localMat4 = localTransform.getWorldMatrix(); const modelMat4 = cameraModelMat4.clone().multiply(localMat4); modelMat4.projectPoint(center, worldCenter); // 包围球中心点正交投影到屏幕上 const vpMat4 = proMat4.clone().multiply(viewMat4); const mvpMat4 = vpMat4.clone().multiply(modelMat4); const screenCenter = mvpMat4.projectPoint(center, new Vector3()); // const screenCenterX = screenCenter.x; // const screenCenterY = screenCenter.y; // 包围球上的点正交投影到屏幕上 const radius = bounding.radius; const up = new Vector3( localMat4.elements[1], localMat4.elements[5], localMat4.elements[9], ).normalize(); const point = up.clone().multiply(radius); point.add(center); const screenPoint = mvpMat4.projectPoint(point, new Vector3()); // const screenPointX = screenPoint.x; // const screenPointY = screenPoint.y; // 计算正交投影到屏幕上的包围球的半径 const screenCenter2 = screenCenter.toVector2(); const screenPoint2 = screenPoint.toVector2(); const screenRadius = screenCenter2.distance(screenPoint2); if (pointInCanvas.distance(screenCenter2) < screenRadius * 1.2) { position = worldCenter; } } else { position = ray.intersectSphere({ center: worldCenter, radius: bounding.radius }, new Vector3()); } break; case BoundingType.triangle: {// 三角数组包围盒 const { triangles, backfaceCulling } = bounding; const tmpPositions: Vector3[] = []; let tmpPosition; for (let j = 0; j < triangles.length; j++) { const triangle = triangles[j]; const worldTriangle: TriangleLike = { p0: worldMat4.projectPoint(triangle.p0 as Vector3, new Vector3()), p1: worldMat4.projectPoint(triangle.p1 as Vector3, new Vector3()), p2: worldMat4.projectPoint(triangle.p2 as Vector3, new Vector3()), }; tmpPosition = ray.intersectTriangle(worldTriangle, new Vector3(), backfaceCulling); if (tmpPosition) { tmpPositions.push(tmpPosition); } } // 仅保存距离相机最近的点 if (tmpPositions.length > 0) { let lastDistance: number; tmpPositions.forEach(item => { const distance = ray.origin.distanceSquared(item); if (!lastDistance || distance < lastDistance) { lastDistance = distance; position = item; } }); } break; } case BoundingType.line: {// 线条包围盒,将线条转换到屏幕空间,计算线条与屏幕交互点的距离,小于阈值判定为点中 const { points, width } = bounding; position = intersectRayLine(new Vector3(), pointInCanvas, points, width / 2, worldMat4, self.composition!.camera); break; } default: break; } if (position) { hitPositions.push(position); // 缓存距离相机最近的 BoundingKey if (gizmoComponent.hitBounding) { const distance = ray.origin.distanceSquared(position); const lastDistance = ray.origin.distanceSquared(gizmoComponent.hitBounding.position); if (distance < lastDistance) { gizmoComponent.hitBounding.key = key; gizmoComponent.hitBounding.position = position; } } else { gizmoComponent.hitBounding = { key, position, }; } } } if (hitPositions.length === 0) { gizmoComponent.hitBounding = undefined; } return hitPositions; }, behavior: spec.InteractBehavior.NOTIFY, }; } }
/** * 射线检测算法 * @returns 包含射线检测算法回调方法的参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/gizmo-component.ts
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
EditorGizmoPlugin.onCompositionItemRemoved
override onCompositionItemRemoved (composition: Composition, item: VFXItem) { if (item.type === GizmoVFXItemType) { this.removeGizmoItem(composition, item); } else { const gizmoVFXItemList: VFXItem[] = composition.loaderData.gizmoTarget[item.id]; if (gizmoVFXItemList && gizmoVFXItemList.length > 0) { gizmoVFXItemList.forEach(gizmoVFXItem => { this.removeGizmoItem(composition, gizmoVFXItem); }); } } }
// }
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/gizmo-loader.ts#L124-L136
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
createRotationMesh
function createRotationMesh ( engine: Engine, options: MeshOption, boundingMap: Map<string, GizmoItemBounding>, ): Map<Mesh, Transform> { const result: Map<Mesh, Transform> = new Map(); const scale = options.size.scale || 1; const size = { radius: 0.8 * scale, tube: 0.02 * scale, radiusSegments: 10 * scale, widthSegments: 20, heightSegments: 20, }; const boundSize = { ...size, tube: size.tube * 2 }; const name = 'rotation'; const xCircle = createBasicMesh(engine, GizmoSubType.torus, { size, color: color.xAxisColor as vec3, renderMode, name, }, 'hideBack'); const yCircle = createBasicMesh(engine, GizmoSubType.torus, { size, color: color.yAxisColor as vec3, renderMode, name, }, 'hideBack'); const zCircle = createBasicMesh(engine, GizmoSubType.torus, { size, color: color.zAxisColor as vec3, renderMode, name, }, 'hideBack'); const center = createBasicMesh(engine, GizmoSubType.sphere, { size, color: [0.3, 0.3, 0.3], alpha: 0.1, renderMode, name, }, 'blend'); const xCircleBounding = createBasicMesh(engine, GizmoSubType.torus, { size: boundSize, color: color.xAxisColor as vec3, renderMode, }); const yCircleBounding = createBasicMesh(engine, GizmoSubType.torus, { size: boundSize, color: color.yAxisColor as vec3, renderMode, }); const zCircleBounding = createBasicMesh(engine, GizmoSubType.torus, { size: boundSize, color: color.zAxisColor as vec3, renderMode, }); const xCircleTransform = new Transform({ valid: true, }); const yCircleTransform = new Transform({ valid: true, }); const zCircleTransform = new Transform({ valid: true, }); const centerTransform = new Transform({ valid: true, }); xCircleTransform.setRotation(0, 90, 0); yCircleTransform.setRotation(-90, 0, 0); zCircleTransform.setRotation(0, 0, 0); result.set(center, centerTransform); result.set(xCircle, xCircleTransform); result.set(yCircle, yCircleTransform); result.set(zCircle, zCircleTransform); boundingMap.set('xAxis', { type: BoundingType.triangle, triangles: xCircleBounding.firstGeometry() ? getTriangle(xCircleBounding.firstGeometry(), xCircleTransform) : [], backfaceCulling: true, }); boundingMap.set('yAxis', { type: BoundingType.triangle, triangles: yCircleBounding.firstGeometry() ? getTriangle(yCircleBounding.firstGeometry(), yCircleTransform) : [], backfaceCulling: true, }); boundingMap.set('zAxis', { type: BoundingType.triangle, triangles: zCircleBounding.firstGeometry() ? getTriangle(zCircleBounding.firstGeometry(), zCircleTransform) : [], backfaceCulling: true, }); return result; }
/** * 创建旋转组件 Mesh 数组 * @internal * @param options * @param boundingMap - 包围盒 Map * @returns Mesh 数组和对应的 Transform 组成的 Map */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/mesh.ts#L81-L163
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
getTriangle
function getTriangle (geometry: Geometry, transform: Transform): TriangleLike[] { const result: TriangleLike[] = []; const indices = geometry.getIndexData(); const points = geometry.getAttributeData('a_Position'); const mat4 = transform.getWorldMatrix(); if (points && indices && indices.length > 0) { for (let i = 0; i < indices.length; i += 3) { const i0 = indices[i] * 3; const i1 = indices[i + 1] * 3; const i2 = indices[i + 2] * 3; const p0 = new Vector3(points[i0], points[i0 + 1], points[i0 + 2]); const p1 = new Vector3(points[i1], points[i1 + 1], points[i1 + 2]); const p2 = new Vector3(points[i2], points[i2 + 1], points[i2 + 2]); result.push({ p0: mat4.projectPoint(p0), p1: mat4.projectPoint(p1), p2: mat4.projectPoint(p2), }); } } return result; }
/** * 获取几何体的三角面数组 * @internal * @param geometry - 几何体 * @param transform - 变换式 * @returns 三角面数组 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/mesh.ts#L172-L197
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
createTranslationMesh
function createTranslationMesh (engine: Engine, options: MeshOption, boundingMap: Map<string, GizmoItemBounding>): Map<Mesh, Transform> { const result: Map<Mesh, Transform> = new Map(); const scale = options.size.scale || 1; const axisSize = { width: 0.8 * scale, height: 0.02 * scale, depth: 0.02 * scale, }; const name = 'translation'; const axisMesh = createAxisMesh(engine, axisSize, false, name); for (const [mesh, transform] of axisMesh) { result.set(mesh, transform); } const arrowSize = { radius: 0.05 * scale, height: 0.2 * scale, }; const xArrow = createBasicMesh(engine, GizmoSubType.cone, { size: arrowSize, color: color.xAxisColor as vec3, renderMode, depthTest: true, name, }, 'blend'); const yArrow = createBasicMesh(engine, GizmoSubType.cone, { size: arrowSize, color: color.yAxisColor as vec3, renderMode, depthTest: true, name, }, 'blend'); const zArrow = createBasicMesh(engine, GizmoSubType.cone, { size: arrowSize, color: color.zAxisColor as vec3, renderMode, name, depthTest: true, }, 'blend'); const xArrowTransform = new Transform({ valid: true, }); const yArrowTransform = new Transform({ valid: true, }); const zArrowTransform = new Transform({ valid: true, }); xArrowTransform.setRotation(0, 0, 90); yArrowTransform.setRotation(0, 0, 0); zArrowTransform.setRotation(-90, 0, 0); xArrowTransform.translate(axisSize.width, 0, 0); yArrowTransform.translate(0, axisSize.width, 0); zArrowTransform.translate(0, 0, axisSize.width); const planeSize = { width: 0.2 * scale, height: 0.2 * scale, }; const xyPlane = createBasicMesh(engine, GizmoSubType.plane, { size: planeSize, color: color.zAxisColor as vec3, renderMode, depthTest: true, name, }); const yzPlane = createBasicMesh(engine, GizmoSubType.plane, { size: planeSize, color: color.xAxisColor as vec3, renderMode, depthTest: true, name, }); const zxPlane = createBasicMesh(engine, GizmoSubType.plane, { size: planeSize, color: color.yAxisColor as vec3, renderMode, depthTest: true, name, }); const xyPlaneTransform = new Transform({ valid: true, }); const yzPlaneTransform = new Transform({ valid: true, }); const zxPlaneTransform = new Transform({ valid: true, }); const delta = axisSize.width / 8; xyPlaneTransform.translate(planeSize.width / 2 + delta, planeSize.height / 2 + delta, 0); yzPlaneTransform.setRotation(0, 90, 0); yzPlaneTransform.translate(0, planeSize.height / 2 + delta, planeSize.width / 2 + delta); zxPlaneTransform.setRotation(-90, 0, 0); zxPlaneTransform.translate(planeSize.width / 2 + delta, 0, planeSize.height / 2 + delta); result.set(xArrow, xArrowTransform); result.set(yArrow, yArrowTransform); result.set(zArrow, zArrowTransform); result.set(xyPlane, xyPlaneTransform); result.set(yzPlane, yzPlaneTransform); result.set(zxPlane, zxPlaneTransform); createAxisBounding({ width: arrowSize.radius * 2.4, length: axisSize.width + arrowSize.height / 2 }, boundingMap); boundingMap.set('xyPlane', { type: BoundingType.triangle, triangles: xyPlane.firstGeometry() ? getTriangle(xyPlane.firstGeometry(), xyPlaneTransform) : [], backfaceCulling: false, }); // FIXME: getTriangle 函数调用 getWorldMatrix 后 worldMatrix被修改 boundingMap.set('yzPlane', { type: BoundingType.triangle, triangles: yzPlane.firstGeometry() ? getTriangle(yzPlane.firstGeometry(), yzPlaneTransform) : [], backfaceCulling: false, }); boundingMap.set('zxPlane', { type: BoundingType.triangle, triangles: zxPlane.firstGeometry() ? getTriangle(zxPlane.firstGeometry(), zxPlaneTransform) : [], backfaceCulling: false, }); return result; }
/** * 创建位移组件 Mesh 数组 * @internal * @param options * @param boundingMap - 包围盒 Map * @returns Mesh 数组和对应的 Transform 组成的 Map */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/mesh.ts#L206-L320
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
createScaleMesh
function createScaleMesh (engine: Engine, options: MeshOption, boundingMap: Map<string, GizmoItemBounding>): Map<Mesh, Transform> { const result: Map<Mesh, Transform> = new Map(); const scale = options.size.scale || 1; const axisSize = { width: 0.8 * scale, height: 0.02 * scale, depth: 0.02 * scale, }; const name = 'scale'; const axisMesh = createAxisMesh(engine, axisSize, false, name); for (const [mesh, transform] of axisMesh) { result.set(mesh, transform); } const boxSize = { width: 0.1 * scale, height: 0.1 * scale, depth: 0.1 * scale, }; const centerBoxSize = { width: 0.2 * scale, height: 0.2 * scale, depth: 0.2 * scale, }; const xCube = createBasicMesh(engine, GizmoSubType.box, { size: boxSize, color: color.xAxisColor as vec3, renderMode, depthTest: true, name, }, 'blend'); const yCube = createBasicMesh(engine, GizmoSubType.box, { size: boxSize, color: color.yAxisColor as vec3, renderMode, depthTest: true, name, }, 'blend'); const zCube = createBasicMesh(engine, GizmoSubType.box, { size: boxSize, color: color.zAxisColor as vec3, renderMode, depthTest: true, name, }, 'blend'); const centerCube = createBasicMesh(engine, GizmoSubType.box, { size: centerBoxSize, color: [0.8, 0.8, 0.8], renderMode, depthTest: true, name, }, 'blend'); const xCubeTransform = new Transform({ valid: true, }); const yCubeTransform = new Transform({ valid: true, }); const zCubeTransform = new Transform({ valid: true, }); const centerCubeTransform = new Transform({ valid: true, }); xCubeTransform.translate(axisSize.width, 0, 0); yCubeTransform.translate(0, axisSize.width, 0); zCubeTransform.translate(0, 0, axisSize.width); result.set(centerCube, centerCubeTransform); result.set(xCube, xCubeTransform); result.set(yCube, yCubeTransform); result.set(zCube, zCubeTransform); createAxisBounding({ width: boxSize.width * 1.2, length: axisSize.width + boxSize.width / 2 }, boundingMap); boundingMap.set('center', { type: BoundingType.sphere, center: new Vector3(), radius: Math.pow(Math.pow(centerBoxSize.width, 2) + Math.pow(centerBoxSize.height, 2) + Math.pow(centerBoxSize.depth, 2), 0.5) / 2, }); return result; }
/** * 创建缩放组件 Mesh 数组 * @internal * @param options * @param boundingMap - 包围盒 Map * @returns Mesh 数组和对应的 Transform 组成的 Map */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/mesh.ts#L329-L398
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
createViewHelperMesh
function createViewHelperMesh (engine: Engine, options: MeshOption, boundingMap: Map<string, GizmoItemBounding>, texture: Map<string, Texture>): Map<Mesh, Transform> { const result: Map<Mesh, Transform> = new Map(); const scale = options.size.scale || 1; const axisSize = { width: 0.8 * scale, height: 0.02 * scale, depth: 0.02 * scale, }; const axisMesh = createAxisMesh(engine, axisSize, true); for (const [mesh, transform] of axisMesh) { result.set(mesh, transform); } const planeSize = { width: 0.14 * scale, height: 0.14 * scale, }; const length = (axisSize.width + planeSize.width) / 2; const depthTest = true; const posXSphere = createSpriteMesh(engine, { size: planeSize, renderMode, depthTest }, texture.get('posX')); const posYSphere = createSpriteMesh(engine, { size: planeSize, renderMode, depthTest }, texture.get('posY')); const posZSphere = createSpriteMesh(engine, { size: planeSize, renderMode, depthTest }, texture.get('posZ')); const posXSphereTransform = new Transform({ valid: true, }); const posYSphereTransform = new Transform({ valid: true, }); const posZSphereTransform = new Transform({ valid: true, }); posXSphereTransform.translate(length, 0, 0); posYSphereTransform.translate(0, length, 0); posZSphereTransform.translate(0, 0, length); const negXSphere = createSpriteMesh(engine, { size: planeSize, renderMode, depthTest }, texture.get('negX')); const negYSphere = createSpriteMesh(engine, { size: planeSize, renderMode, depthTest }, texture.get('negY')); const negZSphere = createSpriteMesh(engine, { size: planeSize, renderMode, depthTest }, texture.get('negZ')); const negXSphereTransform = new Transform({ valid: true, }); const negYSphereTransform = new Transform({ valid: true, }); const negZSphereTransform = new Transform({ valid: true, }); negXSphereTransform.translate(-length, 0, 0); negYSphereTransform.translate(0, -length, 0); negZSphereTransform.translate(0, 0, -length); result.set(posXSphere, posXSphereTransform); result.set(posYSphere, posYSphereTransform); result.set(posZSphere, posZSphereTransform); result.set(negXSphere, negXSphereTransform); result.set(negYSphere, negYSphereTransform); result.set(negZSphere, negZSphereTransform); const radius = (planeSize.height / 2) * 1.2; boundingMap.set('posX', { type: BoundingType.sphere, center: posXSphereTransform.position, radius: radius, }); boundingMap.set('posY', { type: BoundingType.sphere, center: posYSphereTransform.position, radius: radius, }); boundingMap.set('posZ', { type: BoundingType.sphere, center: posZSphereTransform.position, radius: radius, }); boundingMap.set('negX', { type: BoundingType.sphere, center: negXSphereTransform.position, radius: radius, }); boundingMap.set('negY', { type: BoundingType.sphere, center: negYSphereTransform.position, radius: radius, }); boundingMap.set('negZ', { type: BoundingType.sphere, center: negZSphereTransform.position, radius: radius, }); return result; }
/** * 创建视图辅助组件 Mesh 数组 * @internal * @param options * @param boundingMap - 包围盒 Map * @returns Mesh 数组和对应的 Transform 组成的 Map */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/mesh.ts#L407-L500
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
createAxisMesh
function createAxisMesh (engine: Engine, axisSize: { width: number, height: number, depth: number }, isCentered = false, name?: string): Map<Mesh, Transform> { const result: Map<Mesh, Transform> = new Map(); const xAxis = createBasicMesh(engine, GizmoSubType.box, { size: axisSize, color: color.xAxisColor as vec3, renderMode, depthTest: true, name, }); const yAxis = createBasicMesh(engine, GizmoSubType.box, { size: axisSize, color: color.yAxisColor as vec3, renderMode, depthTest: true, name, }); const zAxis = createBasicMesh(engine, GizmoSubType.box, { size: axisSize, color: color.zAxisColor as vec3, renderMode, depthTest: true, name, }); const xAxisTransform = new Transform({ valid: true, }); const yAxisTransform = new Transform({ valid: true, }); const zAxisTransform = new Transform({ valid: true, }); yAxisTransform.setRotation(0, 0, 90); zAxisTransform.setRotation(0, -90, 0); if (!isCentered) { xAxisTransform.translate(axisSize.width / 2, 0, 0); yAxisTransform.translate(0, axisSize.width / 2, 0); zAxisTransform.translate(0, 0, axisSize.width / 2); } result.set(xAxis, xAxisTransform); result.set(yAxis, yAxisTransform); result.set(zAxis, zAxisTransform); return result; }
/** * 创建坐标轴 Mesh 数组 * @internal * @param axisSize - 坐标轴尺寸 * @param isCentered - 是否居中,默认 false * @returns Mesh 数组和对应的 Transform 组成的 Map */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/mesh.ts#L509-L543
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
createBasicMesh
function createBasicMesh (engine: Engine, subType: GizmoSubType, options: MeshOption, materialType = 'default'): Mesh { const { size, color, alpha, renderMode, depthTest, name, } = options; let geometryType: GeometryType; switch (subType) { case GizmoSubType.sphere: geometryType = GeometryType.Sphere; break; case GizmoSubType.cylinder: geometryType = GeometryType.Cylinder; break; case GizmoSubType.cone: geometryType = GeometryType.Cone; break; case GizmoSubType.torus: geometryType = GeometryType.Torus; break; case GizmoSubType.sprite: geometryType = GeometryType.Sprite; break; case GizmoSubType.plane: geometryType = GeometryType.Plane; break; case GizmoSubType.frustum: geometryType = GeometryType.Frustum; break; case GizmoSubType.directionLight: geometryType = GeometryType.DirectionLight; break; case GizmoSubType.spotLight: geometryType = GeometryType.SpotLight; break; case GizmoSubType.pointLight: geometryType = GeometryType.PointLight; break; case GizmoSubType.floorGrid: geometryType = GeometryType.FloorGrid; break; default: geometryType = GeometryType.Box; break; } const geometry = createGeometry(engine, geometryType, size, renderMode); let material; switch (materialType) { case 'blend': material = createBlendMaterial(engine, color, depthTest, alpha); break; case 'hideBack': material = createHideBackMaterial(engine, color, depthTest); break; default: material = createMaterial(engine, color, depthTest); break; } return Mesh.create( engine, { name: name ? name : geometryType.toString(), geometry, material, priority: 3000, }); }
/** * 创建基础几何体 Mesh * @internal * @param subType - gizmoSubType 类型 * @param options - 几何体参数 * @returns 几何体 Mesh */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/mesh.ts#L552-L634
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
createSpriteMesh
function createSpriteMesh (engine: Engine, options: MeshOption, texture?: Texture, boundingMap?: Map<string, GizmoItemBounding>): Mesh { const geometry = createGeometry(engine, GeometryType.Sprite, options.size, renderMode); let spriteColor; if (texture) { spriteColor = texture; } else { spriteColor = options.color; } const material = createSpriteMaterial(engine, spriteColor, options.depthTest); if (boundingMap) { boundingMap.set('sprite', { type: BoundingType.sphere, radius: Math.pow(Math.pow(options.size.width, 2) + Math.pow(options.size.height, 2), 0.5) / 2, }); } return Mesh.create( engine, { name: options.name || 'sprite', geometry, material, priority: 3000, }); }
/** * @internal * @param options * @param texture * @param boundingMap * @returns */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/mesh.ts#L643-L670
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
createMaterial
function createMaterial (engine: Engine, color?: vec3, depthTest?: boolean): Material { const myColor = color ? Vector3.fromArray(color) : new Vector3(255, 255, 255); const myDepthTest = depthTest ? depthTest : false; const material = Material.create( engine, { uniformValues: { u_color: new Float32Array(myColor.toArray()), u_model: new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]), }, uniformSemantics: { u_viewinverse: 'VIEWINVERSE', u_v: 'VIEW', u_p: 'PROJECTION', u_et: 'EDITOR_TRANSFORM', }, shader: { fragment: ` precision mediump float; uniform vec3 u_color; void main(){ gl_FragColor = vec4(u_color,1.0); }`, vertex: ` precision highp float; attribute vec3 a_Position; uniform mat4 u_model; uniform mat4 effects_MatrixV; uniform mat4 _MatrixP; uniform vec4 uEditorTransform; void main(){ gl_Position = _MatrixP * effects_MatrixV * u_model * vec4(a_Position,1.0); gl_Position = vec4(gl_Position.xy * uEditorTransform.xy + uEditorTransform.zw * gl_Position.w,gl_Position.zw); }`, shared: true, }, }); material.setVector3('u_color', myColor); material.setMatrix('u_model', Matrix4.IDENTITY); material.depthTest = myDepthTest; material.stencilTest = false; material.blending = false; material.depthMask = true; return material; }
/** * 创建材质 * @internal * @param color - 颜色 * @param depthTest * @returns 纯色材质,无光照,无透明 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/mesh.ts#L679-L731
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
createHideBackMaterial
function createHideBackMaterial (engine: Engine, color?: vec3, depthTest?: boolean): Material { const myColor = color ? Vector3.fromArray(color) : new Vector3(255, 255, 255); const myDepthTest = depthTest ? depthTest : false; const material = Material.create( engine, { uniformValues: { u_color: new Float32Array(myColor.toArray()), u_model: new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]), u_cameraPos: new Float32Array([0, 0, 0]), u_center: new Float32Array([0, 0, 0]), }, uniformSemantics: { u_viewinverse: 'VIEWINVERSE', u_v: 'VIEW', u_p: 'PROJECTION', u_et: 'EDITOR_TRANSFORM', }, shader: { fragment: ` precision mediump float; uniform vec3 u_color; varying float flag; void main(){ if(flag > 0.0) { discard; } gl_FragColor = vec4(u_color,1.0); }`, vertex: ` precision highp float; attribute vec3 a_Position; uniform mat4 u_model; uniform mat4 effects_MatrixV; uniform mat4 _MatrixP; uniform vec4 uEditorTransform; uniform vec3 u_cameraPos; uniform vec3 u_center; varying float flag; void main(){ float distance = length(u_cameraPos - u_center); vec3 worldPosition = (u_model * vec4(a_Position,1.0)).xyz; flag = length(u_cameraPos - worldPosition) - distance; gl_Position = _MatrixP * effects_MatrixV * u_model * vec4(a_Position,1.0); gl_Position = vec4(gl_Position.xy * uEditorTransform.xy + uEditorTransform.zw * gl_Position.w,gl_Position.zw); }`, shared: true, }, }); material.setVector3('u_color', myColor); material.setMatrix('u_model', Matrix4.IDENTITY); material.setVector3('u_cameraPos', Vector3.ZERO); material.setVector3('u_center', Vector3.ZERO); material.depthTest = myDepthTest; material.stencilTest = false; material.blending = false; material.depthMask = true; return material; }
/** * * @internal * @param color * @param depthTest * @returns */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/mesh.ts#L740-L810
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
createBlendMaterial
function createBlendMaterial (engine: Engine, color?: vec3, depthTest?: boolean, alpha?: number): Material { const myColor = color ? Vector3.fromArray(color) : new Vector3(255, 255, 255); const myDepthTest = depthTest ? depthTest : false; const myAlpha = alpha ? alpha : 1; const material = Material.create( engine, { uniformValues: { u_color: new Float32Array(myColor.toArray()), u_alpha: new Float32Array([myAlpha, 0]), u_model: new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]), }, uniformSemantics: { u_viewinverse: 'VIEWINVERSE', u_v: 'VIEW', u_p: 'PROJECTION', u_et: 'EDITOR_TRANSFORM', }, shader: { shared: true, fragment: ` precision mediump float; uniform vec3 u_color; uniform vec2 u_alpha; void main(){ gl_FragColor = vec4(u_color,u_alpha); }`, vertex: ` precision highp float; attribute vec3 a_Position; uniform mat4 u_model; uniform mat4 effects_MatrixV; uniform mat4 _MatrixP; uniform vec4 uEditorTransform; void main(){ gl_Position = _MatrixP * effects_MatrixV * u_model * vec4(a_Position,1.0); gl_Position = vec4(gl_Position.xy * uEditorTransform.xy + uEditorTransform.zw * gl_Position.w,gl_Position.zw); }`, }, }); material.setVector3('u_color', myColor); material.setVector2('u_alpha', new Vector2(myAlpha, 0)); material.setMatrix('u_model', Matrix4.IDENTITY); material.depthTest = myDepthTest; material.stencilTest = false; material.blending = true; material.blendEquation = [glContext.FUNC_ADD, glContext.FUNC_ADD]; material.blendFunction = [glContext.SRC_ALPHA, glContext.ONE_MINUS_SRC_ALPHA, glContext.SRC_ALPHA, glContext.ONE_MINUS_SRC_ALPHA]; material.depthMask = true; return material; }
/** * * @internal * @param color * @param depthTest * @param alpha * @returns */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/mesh.ts#L820-L877
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
createAxisBounding
function createAxisBounding (size: { width: number, length: number }, boundingMap: Map<string, GizmoItemBounding>) { boundingMap.set('xAxis', { type: BoundingType.line, points: [new Vector3(0, 0, 0), new Vector3(size.length, 0, 0)], width: size.width, }); boundingMap.set('yAxis', { type: BoundingType.line, points: [new Vector3(0, 0, 0), new Vector3(0, size.length, 0)], width: size.width, }); boundingMap.set('zAxis', { type: BoundingType.line, points: [new Vector3(0, 0, 0), new Vector3(0, 0, size.length)], width: size.width, }); }
/** * 创建坐标轴包围盒 * @internal * @param size 包围盒尺寸 * @param boundingMap 包围盒 Map */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/mesh.ts#L973-L989
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
BoxGeometryData.constructor
constructor (width = 1, height = 1, depth = 1) { super(); let numberOfVertices = 0; buildPlane(2, 1, 0, - 1, - 1, depth, height, width, this.points, this.uvs, this.normals, this.indices); // px buildPlane(2, 1, 0, 1, - 1, depth, height, - width, this.points, this.uvs, this.normals, this.indices); // nx buildPlane(0, 2, 1, 1, 1, width, depth, height, this.points, this.uvs, this.normals, this.indices); // py buildPlane(0, 2, 1, 1, - 1, width, depth, - height, this.points, this.uvs, this.normals, this.indices); // ny buildPlane(0, 1, 2, 1, - 1, width, height, depth, this.points, this.uvs, this.normals, this.indices); // pz buildPlane(0, 1, 2, - 1, - 1, width, height, - depth, this.points, this.uvs, this.normals, this.indices); // nz function buildPlane (u: number, v: number, w: number, uDir: number, vDir: number, width: number, height: number, depth: number, points: number[], uvs: number[], normals: number[], indices: number[]) { const widthHalf = width / 2; const heightHalf = height / 2; const depthHalf = depth / 2; const point: spec.vec3 = [0, 0, 0]; let vertexCounter = 0; for (let iy = 0; iy < 2; iy++) { const y = iy * height - heightHalf; for (let ix = 0; ix < 2; ix++) { const x = ix * width - widthHalf; point[u] = x * uDir; point[v] = y * vDir; point[w] = depthHalf; points.push(...point); point[u] = 0; point[v] = 0; point[w] = depth > 0 ? 1 : - 1; normals.push(...point); uvs.push(ix); uvs.push(1 - iy); vertexCounter += 1; } } for (let iy = 0; iy < 1; iy++) { for (let ix = 0; ix < 1; ix++) { const a = numberOfVertices + ix + 2 * iy; const b = numberOfVertices + ix + 2 * (iy + 1); const c = numberOfVertices + (ix + 1) + 2 * (iy + 1); const d = numberOfVertices + (ix + 1) + 2 * iy; indices.push(a, b, d); indices.push(b, c, d); } } numberOfVertices += vertexCounter; } }
/** * * @param width * @param height * @param depth */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/box.ts#L12-L73
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
buildPlane
function buildPlane (u: number, v: number, w: number, uDir: number, vDir: number, width: number, height: number, depth: number, points: number[], uvs: number[], normals: number[], indices: number[]) { const widthHalf = width / 2; const heightHalf = height / 2; const depthHalf = depth / 2; const point: spec.vec3 = [0, 0, 0]; let vertexCounter = 0; for (let iy = 0; iy < 2; iy++) { const y = iy * height - heightHalf; for (let ix = 0; ix < 2; ix++) { const x = ix * width - widthHalf; point[u] = x * uDir; point[v] = y * vDir; point[w] = depthHalf; points.push(...point); point[u] = 0; point[v] = 0; point[w] = depth > 0 ? 1 : - 1; normals.push(...point); uvs.push(ix); uvs.push(1 - iy); vertexCounter += 1; } } for (let iy = 0; iy < 1; iy++) { for (let ix = 0; ix < 1; ix++) { const a = numberOfVertices + ix + 2 * iy; const b = numberOfVertices + ix + 2 * (iy + 1); const c = numberOfVertices + (ix + 1) + 2 * (iy + 1); const d = numberOfVertices + (ix + 1) + 2 * iy; indices.push(a, b, d); indices.push(b, c, d); } } numberOfVertices += vertexCounter; }
// nz
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/box.ts#L24-L72
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ConeGeometryData.constructor
constructor ( radius = 1, height = 1, radialSegments = 8, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2, ) { super(0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength); }
/** * * @param radius * @param height * @param radialSegments * @param heightSegments * @param openEnded * @param thetaStart * @param thetaLength */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/cone.ts#L17-L27
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
CylinderGeometryData.constructor
constructor ( radiusTop = 1, radiusBottom = 1, height = 1, radialSegments = 8, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2, ) { super(); radialSegments = Math.floor(radialSegments); heightSegments = Math.floor(heightSegments); let index = 0; const indexArray: number[][] = []; const halfHeight = height / 2; generateTorso(this.points, this.uvs, this.normals, this.indices); if (openEnded === false) { if (radiusTop > 0) { generateCap(true, this.points, this.uvs, this.normals, this.indices); } if (radiusBottom > 0) { generateCap(false, this.points, this.uvs, this.normals, this.indices); } } function generateTorso (points: number[], uvs: number[], normals: number[], indices: number[]) { // this will be used to calculate the normal const slope = (radiusBottom - radiusTop) / height; for (let y = 0; y <= heightSegments; y++) { const indexRow = []; const v = y / heightSegments; const radius = v * (radiusBottom - radiusTop) + radiusTop; for (let x = 0; x <= radialSegments; x++) { const u = x / radialSegments; const theta = u * thetaLength + thetaStart; const sinTheta = Math.sin(theta); const cosTheta = Math.cos(theta); const px = radius * sinTheta; const py = - v * height + halfHeight; const pz = radius * cosTheta; points.push(px, py, pz); const normal = new Vector3(sinTheta, slope, cosTheta).normalize(); normals.push(...normal.toArray()); uvs.push(u, 1 - v); indexRow.push(index++); } indexArray.push(indexRow); } for (let x = 0; x < radialSegments; x++) { for (let y = 0; y < heightSegments; y++) { const a = indexArray[y][x]; const b = indexArray[y + 1][x]; const c = indexArray[y + 1][x + 1]; const d = indexArray[y][x + 1]; indices.push(a, b, d); indices.push(b, c, d); } } } function generateCap (top: boolean, points: number[], uvs: number[], normals: number[], indices: number[]) { const centerIndexStart = index; const radius = (top === true) ? radiusTop : radiusBottom; const sign = (top === true) ? 1 : - 1; for (let x = 1; x <= radialSegments; x++) { points.push(0, halfHeight * sign, 0); normals.push(0, sign, 0); uvs.push(0.5, 0.5); index++; } const centerIndexEnd = index; for (let x = 0; x <= radialSegments; x++) { const u = x / radialSegments; const theta = u * thetaLength + thetaStart; const cosTheta = Math.cos(theta); const sinTheta = Math.sin(theta); const px = radius * sinTheta; const py = halfHeight * sign; const pz = radius * cosTheta; points.push(px, py, pz); normals.push(0, sign, 0); const uvU = (cosTheta * 0.5) + 0.5; const uvV = (sinTheta * 0.5 * sign) + 0.5; uvs.push(uvU, uvV); index++; } for (let x = 0; x < radialSegments; x++) { const c = centerIndexStart + x; const i = centerIndexEnd + x; if (top === true) { indices.push(i, i + 1, c); } else { indices.push(i + 1, i, c); } } } }
/** * * @param radiusTop * @param radiusBottom * @param height * @param radialSegments * @param heightSegments * @param openEnded * @param thetaStart * @param thetaLength */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/cylinder.ts#L21-L143
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
DirectionLightData.constructor
constructor ( radius = 1, length = 2, segments = 64, linesNumber = 8, thetaStart = 0, thetaLength = Math.PI * 2, ) { super(); this.createXYPlaneCircle(radius, 0, segments, thetaStart, thetaLength, 0, this.points, this.indices); const start = segments % linesNumber / 2; const step = segments / linesNumber; for (let i = 0; i < linesNumber; i++) { const index = start + i * step; const x = this.points[3 * index]; const y = this.points[3 * index + 1]; const z = -length; this.points.push(x, y, z); this.indices.push(index, segments + i); } }
/** * 构造函数 * @param radius - 圆半径 * @param length - 光线长度 * @param segments - 圆精度 * @param linesNumber - 光线数量 * @param thetaStart - 起始弧度 * @param thetaLength - 终止弧度 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/direction-light.ts#L17-L40
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
FloorLineEdgeGeometryData.constructor
constructor (sideLength = 12, parts = 6) { super(); let index = 0; const z = 0; const end = sideLength / 2; const start = -sideLength / 2; for (let i = 0; i <= parts; i++) { const v = start + i * sideLength / parts; this.points.push(v, start, z, v, end, z, start, v, z, end, v, z); this.indices.push(index, index + 1, index + 2, index + 3); index += 4; } }
/** * * @param sideLength * @param parts */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/floor-line-edge.ts#L13-L29
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
FloorLineGeometryData.constructor
constructor (sideLength = 12, parts = 6, subparts = 5) { super(); const start = -sideLength / 2; const end = sideLength / 2; let index = 0; const z = 0; for (let i = 0; i <= parts; i++) { for (let j = 1; j < subparts; j++) { const partLength = sideLength / parts; const v = start + i * partLength + j * partLength / 5; this.points.push(v, start, z, v, end, z, start, v, z, end, v, z); this.indices.push(index, index + 1, index + 2, index + 3); index += 4; } } }
/** * * @param sideLength * @param parts * @param subparts */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/floor-line.ts#L14-L32
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
FrustumGeometryData.constructor
constructor ( position: spec.vec3, rotation: spec.vec3, fov: number, aspect: number, near: number, far: number, ) { super(); const fovY = fov / 180 * Math.PI; const ratio = aspect; const halfY = far * Math.tan(fovY / 2); const halfX = halfY * ratio; const point1 = new Vector3(halfX, halfY, far); const point2 = new Vector3(-halfX, halfY, far); const point3 = new Vector3(-halfX, -halfY, far); const point4 = new Vector3(halfX, -halfY, far); const matrix = Euler.fromArray(rotation).toMatrix4(new Matrix4()); const result1 = matrix.transformPoint(point1).add(position); const result2 = matrix.transformPoint(point2).add(position); const result3 = matrix.transformPoint(point3).add(position); const result4 = matrix.transformPoint(point4).add(position); this.points.push( ...position, ...result1.toArray(), ...result2.toArray(), ...result3.toArray(), ...result4.toArray() ); this.indices.push(0, 1, 0, 2, 0, 3, 0, 4, 1, 2, 2, 3, 3, 4, 4, 1); }
/** * * @param position * @param rotation * @param fov * @param aspect * @param near * @param far */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/frustum.ts#L20-L50
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
PlaneGeometryData.constructor
constructor ( width = 1, height = 1, widthSegments = 1, heightSegments = 1, ) { super(); const widthHalf = width / 2; const heightHalf = height / 2; const gridX = Math.floor(widthSegments); const gridY = Math.floor(heightSegments); const gridX1 = gridX + 1; const gridY1 = gridY + 1; const segmentWidth = width / gridX; const segmentHeight = height / gridY; for (let iy = 0; iy < gridY1; iy++) { const y = iy * segmentHeight - heightHalf; for (let ix = 0; ix < gridX1; ix++) { const x = ix * segmentWidth - widthHalf; this.points.push(x, - y, 0); this.normals.push(0, 0, 1); this.uvs.push(ix / gridX); this.uvs.push(1 - (iy / gridY)); } } for (let iy = 0; iy < gridY; iy++) { for (let ix = 0; ix < gridX; ix++) { const a = ix + gridX1 * iy; const b = ix + gridX1 * (iy + 1); const c = (ix + 1) + gridX1 * (iy + 1); const d = (ix + 1) + gridX1 * iy; this.indices.push(a, b, d); this.indices.push(b, c, d); } } }
/** * * @param width * @param height * @param widthSegments * @param heightSegments */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/plane.ts#L14-L57
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
PointLightData.constructor
constructor ( range = 10, segments = 64, thetaStart = 0, thetaLength = Math.PI * 2, ) { super(); this.createXYPlaneCircle(range, 0, segments, thetaStart, thetaLength, 0, this.points, this.indices); this.createXZPlaneCircle(range, 0, segments, thetaStart, thetaLength, segments, this.points, this.indices); this.createYZPlaneCircle(range, 0, segments, thetaStart, thetaLength, 2 * segments, this.points, this.indices); }
/** * 构造函数 * @param range - 光照范围 * @param segments - 圆精度 * @param thetaStart - 起始弧度 * @param thetaLength - 终止弧度 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/point-light.ts#L15-L25
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
SphereGeometryData.constructor
constructor ( radius = 1, widthSegments = 8, heightSegments = 6, phiStart = 0, phiLength = Math.PI * 2, thetaStart = 0, thetaLength = Math.PI, ) { super(); // 限制至少需要分为六块以保持形状 widthSegments = Math.max(3, Math.floor(widthSegments)); heightSegments = Math.max(2, Math.floor(heightSegments)); const thetaEnd = Math.min(thetaStart + thetaLength, Math.PI); const grid = []; let index = 0; for (let iy = 0; iy <= heightSegments; iy++) { const verticesRow = []; const v = iy / heightSegments; let uOffset = 0; if (iy == 0 && thetaStart == 0) { uOffset = 0.5 / widthSegments; } else if (iy == heightSegments && thetaEnd == Math.PI) { uOffset = - 0.5 / widthSegments; } for (let ix = 0; ix <= widthSegments; ix++) { const u = ix / widthSegments; // point const x = - radius * Math.cos(phiStart + u * phiLength) * Math.sin(thetaStart + v * thetaLength); const y = radius * Math.cos(thetaStart + v * thetaLength); const z = radius * Math.sin(phiStart + u * phiLength) * Math.sin(thetaStart + v * thetaLength); this.points.push(x, y, z); // normal const normal = new Vector3(x, y, z).normalize(); this.normals.push(...normal.toArray()); // uv this.uvs.push(u + uOffset, 1 - v); verticesRow.push(index++); } grid.push(verticesRow); } // indices for (let iy = 0; iy < heightSegments; iy++) { for (let ix = 0; ix < widthSegments; ix++) { const a = grid[iy][ix + 1]; const b = grid[iy][ix]; const c = grid[iy + 1][ix]; const d = grid[iy + 1][ix + 1]; if (iy !== 0 || thetaStart > 0) { this.indices.push(a, b, d); } if (iy !== heightSegments - 1 || thetaEnd < Math.PI) { this.indices.push(b, c, d); } } } }
/** * * @param radius * @param widthSegments * @param heightSegments * @param phiStart * @param phiLength * @param thetaStart * @param thetaLength */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/sphere.ts#L20-L87
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
SpotLightData.constructor
constructor ( range = 10, spotAngle = 30, segments = 64, thetaStart = 0, thetaLength = Math.PI * 2, ) { super(); const angle = spotAngle / 2 / 180 * Math.PI; const radius = range * Math.tan(angle); this.createXYPlaneCircle(radius, -range, segments, thetaStart, thetaLength, 0, this.points, this.indices); this.points.push(0, 0, 0); const step = segments / 4; const start = segments % 4 / 2; for (let i = 0; i < 4; i++) { this.indices.push(segments, start + i * step); } }
/** * 构造函数 * @param range - 光线范围 * @param spotAngle - 光线范围[角度] * @param segments - 圆精度 * @param thetaStart - 起始弧度 * @param thetaLength - 终止弧度 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/spot-light.ts#L16-L36
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
TorusGeometryData.constructor
constructor ( radius = 1, tube = 0.4, radialSegments = 64, tubularSegments = 64, arc = Math.PI * 2, ) { super(); radialSegments = Math.floor(radialSegments); tubularSegments = Math.floor(tubularSegments); for (let j = 0; j <= radialSegments; j++) { for (let i = 0; i <= tubularSegments; i++) { const u = i / tubularSegments * arc; const v = j / radialSegments * Math.PI * 2; const px = (radius + tube * Math.cos(v)) * Math.cos(u); const py = (radius + tube * Math.cos(v)) * Math.sin(u); const pz = tube * Math.sin(v); this.points.push(px, py, pz); const cx = radius * Math.cos(u); const cy = radius * Math.sin(u); const p = new Vector3(px, py, pz); const c = new Vector3(cx, cy, 0); const normal = p.subtract(c).normalize(); this.normals.push(...normal.toArray()); this.uvs.push(i / tubularSegments); this.uvs.push(j / radialSegments); } } for (let j = 1; j <= radialSegments; j++) { for (let i = 1; i <= tubularSegments; i++) { const a = (tubularSegments + 1) * j + i - 1; const b = (tubularSegments + 1) * (j - 1) + i - 1; const c = (tubularSegments + 1) * (j - 1) + i; const d = (tubularSegments + 1) * j + i; this.indices.push(a, b, d); this.indices.push(b, c, d); } } }
/** * * @param radius * @param tube * @param radialSegments * @param tubularSegments * @param arc */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/editor-gizmo/src/geometry/torus.ts#L18-L66
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
InputController.registerMouseEvent
private registerMouseEvent (owner: InputController) { // 设备类型过滤,只在windows和mac上注册 const platform = navigator.platform.toLowerCase(); if (platform.search('win') < 0 && platform.search('mac') < 0) { return; } if (this.canvas === undefined || this.gesture === undefined) { return; } this.mouseDownListener = (e: MouseEvent) => { const buttons = e.buttons as MouseButtonType; owner.isMouseDown = true; owner.isMoveBegin = true; if (buttons === MouseButtonType.left) { owner.currentAction = InputActionType.rotatePoint; } else if (buttons === MouseButtonType.middle) { owner.currentAction = InputActionType.moveXYPlane; } else if (buttons === MouseButtonType.right) { owner.currentAction = InputActionType.moveZAxis; } }; this.canvas.addEventListener('mousedown', this.mouseDownListener); this.mouseMoveListener = (e: MouseEvent) => { if (owner.canvas === undefined || owner.gesture === undefined || !owner.isMouseDown) { return; } const buttons = e.buttons as MouseButtonType; if (buttons === MouseButtonType.left) { if (owner.isMoveBegin) { owner.gesture.onRotatePointBegin( e.clientX, e.clientY, owner.canvas.width / 2, owner.canvas.height / 2, [0, 0, 0], owner.cameraID, ); owner.isMoveBegin = false; this.launchBeginEventCallback('RotatePoint'); } owner.gesture.onRotatingPoint(e.clientX, e.clientY, owner.getRotateSpeed()); this.launchMoveEventCallback('RotatePoint'); } else if (buttons === MouseButtonType.middle) { if (owner.isMoveBegin) { owner.gesture.onXYMoveBegin( e.clientX, e.clientY, owner.canvas.width / 2, owner.canvas.height / 2, owner.cameraID, ); owner.isMoveBegin = false; this.launchBeginEventCallback('XYMove'); } owner.gesture.onXYMoving(e.clientX, e.clientY, owner.getMouseMoveSpeed()); this.launchMoveEventCallback('XYMove'); } else if (buttons === MouseButtonType.right) { if (owner.isMoveBegin) { owner.gesture.onZMoveBegin( e.clientX, e.clientY, owner.canvas.width / 2, owner.canvas.height / 2, owner.cameraID, ); owner.isMoveBegin = false; this.launchBeginEventCallback('ZMove'); } owner.gesture.onZMoving(e.clientX, e.clientY, owner.getMouseMoveSpeed()); this.launchMoveEventCallback('ZMove'); } }; this.canvas.addEventListener('mousemove', this.mouseMoveListener); this.mouseUpListener = (e: MouseEvent) => { owner.isMouseDown = false; owner.isMoveBegin = false; if (owner.gesture === undefined) { return; } const buttons = e.buttons as MouseButtonType; if (buttons === MouseButtonType.left) { owner.gesture.onRotatePointEnd(); this.launchEndEventCallback('RotatePoint'); } else if (buttons === MouseButtonType.middle) { owner.gesture.onXYMoveEnd(); this.launchEndEventCallback('XYMove'); } else if (buttons === MouseButtonType.right) { owner.gesture.onZMoveEnd(); this.launchEndEventCallback('ZMove'); } }; this.canvas.addEventListener('mouseup', this.mouseUpListener); this.mouseLeaveListener = (e: MouseEvent) => { owner.isMouseDown = false; owner.isMoveBegin = false; if (owner.gesture === undefined) { return; } const buttons = e.buttons as MouseButtonType; if (buttons === MouseButtonType.left) { owner.gesture.onRotatePointEnd(); this.launchEndEventCallback('RotatePoint'); } else if (buttons === MouseButtonType.middle) { owner.gesture.onXYMoveEnd(); this.launchEndEventCallback('XYMove'); } else if (buttons === MouseButtonType.right) { owner.gesture.onZMoveEnd(); this.launchEndEventCallback('ZMove'); } }; this.canvas.addEventListener('mouseleave', this.mouseLeaveListener); this.mouseWheelListener = (e: WheelEvent) => { if (owner.gesture === undefined) { return; } const we = e ; owner.gesture.onKeyEvent({ cameraID: owner.cameraID, zAxis: we.deltaY > 0 ? 1 : -1, speed: owner.getWheelMoveSpeed(), }); this.launchMoveEventCallback('ZMove'); }; this.canvas.addEventListener('wheel', this.mouseWheelListener); }
/** * 注册鼠标回调事件,只有在PC上才会注册,手机上不会注册 * * @param owner 输入控制器对象 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/gesture/controller.ts#L98-L247
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
InputController.registerTouchEvent
private registerTouchEvent (owner: InputController) { // 设备类型过滤,只在安卓和iPhone上注册 const platform = navigator.platform.toLowerCase(); if (platform.search('arm') < 0 && platform.search('iphone') < 0) { return; } if (this.canvas === undefined) { return; } this.hammer = new Hammer(this.canvas); this.hammer.get('pinch').set({ enable: true }); this.hammer.on('pinch', function (ev) { if (owner.canvas === undefined || owner.gesture === undefined) { return; } if (ev.type === 'pinch') { // @ts-expect-error const additionalEvent = ev.additionalEvent; const speed = Math.sqrt(ev.velocityX * ev.velocityX + ev.velocityY * ev.velocityY) * owner.getKeyMoveSpeed() * 5; if (additionalEvent === 'pinchin') { owner.gesture.onKeyEvent({ cameraID: owner.cameraID, zAxis: 1, speed: speed }); } else if (additionalEvent === 'pinchout') { owner.gesture.onKeyEvent({ cameraID: owner.cameraID, zAxis: -1, speed: speed }); } } }); owner.currentAction = InputActionType.rotatePoint; this.hammer.on('pan panstart panend tap', function (ev) { if (owner.canvas === undefined || owner.gesture === undefined) { return; } if (ev.type === 'panstart') { if (owner.currentAction === InputActionType.rotatePoint) { owner.gesture.onRotatePointBegin( 0, 0, owner.canvas.width / 2, owner.canvas.height / 2, [0, 0, 0], owner.cameraID, ); } else if (owner.currentAction === InputActionType.moveXYPlane) { owner.gesture.onXYMoveBegin( 0, 0, owner.canvas.width / 2, owner.canvas.height / 2, owner.cameraID, ); } else if (owner.currentAction === InputActionType.moveZAxis) { owner.gesture.onZMoveBegin( 0, 0, owner.canvas.width / 2, owner.canvas.height / 2, owner.cameraID, ); } } else if (ev.type === 'pan') { if (owner.currentAction === InputActionType.rotatePoint) { owner.gesture.onRotatingPoint(ev.deltaX, ev.deltaY, owner.getRotateSpeed()); } else if (owner.currentAction === InputActionType.moveXYPlane) { owner.gesture.onXYMoving(ev.deltaX, ev.deltaY, owner.getMouseMoveSpeed() * 0.2); } else if (owner.currentAction === InputActionType.moveZAxis) { owner.gesture.onZMoving(ev.deltaX, ev.deltaY, owner.getMouseMoveSpeed() * 0.2); } } else if (ev.type === 'panend') { if (owner.currentAction === InputActionType.rotatePoint) { owner.gesture.onRotatePointEnd(); } else if (owner.currentAction === InputActionType.moveXYPlane) { owner.gesture.onXYMoveEnd(); } else if (owner.currentAction === InputActionType.moveZAxis) { owner.gesture.onZMoveEnd(); } } else if (ev.type === 'tap') { if (owner.currentAction !== InputActionType.rotatePoint) { owner.currentAction = InputActionType.rotatePoint; } else { owner.currentAction = InputActionType.moveXYPlane; } } }); }
/** * 注册触控回调事件,只在手机上才会注册,PC上不会注册 * * @param owner 输入控制器对象 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/gesture/controller.ts#L298-L384
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
InputController.launchBeginEventCallback
private launchBeginEventCallback (eventName: string) { if (this.beginEventCallback !== undefined) { this.beginEventCallback(eventName); } }
/** * 相机开始事件的回调 * * @param eventName 事件名称 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/gesture/controller.ts#L391-L395
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
InputController.launchMoveEventCallback
private launchMoveEventCallback (eventName: string) { if (this.moveEventCallback !== undefined) { this.moveEventCallback(eventName); } }
/** * 相机持续事件的回调 * * @param eventName 事件名称 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/gesture/controller.ts#L402-L406
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
InputController.launchEndEventCallback
private launchEndEventCallback (eventName: string) { if (this.endEventCallback !== undefined) { this.endEventCallback(eventName); } }
/** * 相机结束事件的回调 * * @param eventName 事件名称 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/gesture/controller.ts#L413-L417
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
LoaderImpl.processLight
processLight (lights: GLTFLight[], fromGLTF: boolean): void { lights.forEach(l => { if (l.color === undefined) { if (fromGLTF) { l.color = [255, 255, 255, 255]; } else { l.color = [1, 1, 1, 1]; } } else { l.color[0] = this.scaleColorVal(l.color[0], fromGLTF); l.color[1] = this.scaleColorVal(l.color[1], fromGLTF); l.color[2] = this.scaleColorVal(l.color[2], fromGLTF); l.color[3] = this.scaleColorVal(l.color[3], fromGLTF); } }); }
/** * for old scene compatibility */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/gltf/loader-impl.ts#L946-L957
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelMeshComponent.constructor
constructor (engine: Engine, data?: ModelMeshComponentData) { super(engine); if (data) { this.fromData(data); } }
/** * 构造函数,只保存传入参数,不在这里创建内部对象 * @param engine - 引擎 * @param data - Mesh 参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L51-L56
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelMeshComponent.onStart
override onStart (): void { this.sceneManager = getSceneManager(this); this.createContent(); this.item.type = VFX_ITEM_TYPE_3D; this.priority = this.item.renderOrder; this.sceneManager?.addItem(this.content); if (this.item.parentId && this.item.parent) { this.content.updateParentInfo(this.item.parentId, this.item.parent); } this.setVisible(true); this.item.getHitTestParams = this.getHitTestParams; }
/** * 组件开始,需要创建内部对象,更新父元素信息和添加到场景管理器中 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L61-L72
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelMeshComponent.onUpdate
override onUpdate (dt: number): void { if (this.sceneManager) { this.content.build(this.sceneManager); } this.content.update(); }
/** * 组件更新,更新内部对象状态 * @param dt - 更新间隔 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L78-L84
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelMeshComponent.onLateUpdate
override onLateUpdate (dt: number): void { this.content.lateUpdate(); }
/** * 组件晚更新,晚更新内部对象状态 * @param dt - 更新间隔 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L90-L92
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelSkyboxComponent.render
override render (renderer: Renderer) { if (!this.getVisible() || !this.sceneManager) { return; } this.content.render(this.sceneManager, renderer); }
/** * 组件渲染,需要检查可见性 * @param renderer - 渲染器 * @returns */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L99-L105
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelSkyboxComponent.onDestroy
override onDestroy (): void { this.sceneManager?.removeItem(this.content); this.sceneManager = undefined; this.content.dispose(); }
/** * 组件销毁,需要重场景管理器中删除 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L110-L114
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelMeshComponent.fromData
override fromData (data: ModelMeshComponentData): void { super.fromData(data); this.data = data; }
/** * 反序列化,记录传入参数 * @param data - 组件参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L120-L123
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelMeshComponent.createContent
createContent () { if (this.data) { const bounding = this.data.interaction; this.bounding = bounding && JSON.parse(JSON.stringify(bounding)); this.content = new PMesh(this.engine, this.item.name, this.data, this, this.item.parentId); } }
/** * 创建内部对象 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L128-L136
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelSkyboxComponent.setVisible
setVisible (visible: boolean) { this.content?.onVisibleChanged(visible); }
/** * 设置当前 Mesh 的可见性。 * @param visible - true:可见,false:不可见 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L142-L144
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelSkyboxComponent.getVisible
getVisible (): boolean { return this.content?.visible ?? false; }
/** * 获取当前 Mesh 的可见性。 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L149-L151
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelMeshComponent.getHitTestParams
getHitTestParams = (force?: boolean): HitTestBoxParams | HitTestSphereParams | HitTestCustomParams | undefined => { this.computeBoundingBox(); const bounding = this.bounding; if (bounding && (force || Number.isInteger(bounding.behavior))) { const type = bounding.type; if (type === spec.ModelBoundingType.box) { if (this.content instanceof PMesh) { const mesh = this.content; const customHitTest: HitTestCustomParams = { behavior: bounding.behavior as number, type: HitTestType.custom, collect: function (ray: Ray, pointInCanvas: Vector2) { const result = mesh.hitTesting(ray.origin, ray.direction); return result; }, }; return customHitTest; } else { const worldMatrixData = this.transform.getWorldMatrix(); const customHitTest: HitTestCustomParams = { behavior: bounding.behavior as number, type: HitTestType.custom, collect: function (ray: Ray, pointInCanvas: Vector2) { const result = RayIntersectsBoxWithRotation(ray, worldMatrixData, bounding); return result; }, }; return customHitTest; } } else if (type === spec.ModelBoundingType.sphere) { const pos = new Vector3(); this.transform.assignWorldTRS(pos); const center = new Vector3(); if (bounding.center) { center.setFromArray(bounding.center); } center.add(pos); return { type: type as unknown as HitTestType.sphere, behavior: bounding.behavior as number, radius: bounding.radius || 0, center, }; } } }
/** * 获取点击测试参数,根据元素包围盒进行相交测试,Mesh 对象会进行更加精确的点击测试 * @param force - 是否强制进行点击测试 * @returns 点击测试参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelMeshComponent.computeBoundingBox
computeBoundingBox (): ModelItemBounding | undefined { if (this.content && this.content instanceof PMesh) { const worldMatrix = this.transform.getWorldMatrix(); const bbox = this.content.computeBoundingBox(worldMatrix); const center = bbox.getCenter(new Vector3()); const size = bbox.getSize(new Vector3()); this.bounding = { behavior: this.bounding?.behavior, type: spec.ModelBoundingType.box, center: [center.x, center.y, center.z], size: [size.x, size.y, size.z], }; return this.bounding; } else { return; } }
/** * 计算元素包围盒,只针对 Mesh 对象 * @returns 包围盒 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L219-L237
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelSkyboxComponent.constructor
constructor (engine: Engine, data?: ModelSkyboxComponentData) { super(engine); if (data) { this.fromData(data); } }
/** * 构造函数,只保存传入参数,不在这里创建内部对象 * @param engine - 引擎 * @param data - Mesh 参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L264-L269
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelSkyboxComponent.onStart
override onStart (): void { this.createContent(); this.item.type = VFX_ITEM_TYPE_3D; this.priority = this.item.renderOrder; this.sceneManager = getSceneManager(this); this.sceneManager?.addItem(this.content); this.setVisible(true); }
/** * 组件开始,需要创建内部对象和添加到场景管理器中 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L274-L281
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelSkyboxComponent.fromData
override fromData (data: ModelSkyboxComponentData): void { super.fromData(data); this.data = data; }
/** * 反序列化,记录传入参数 * @param data - 组件参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L309-L312
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelSkyboxComponent.createContent
createContent () { if (this.data) { const skyboxData = this.data; this.content = new PSkybox(this.item.name, skyboxData, this); } }
/** * 创建内部对象 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L317-L323
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelLightComponent.constructor
constructor (engine: Engine, data?: ModelLightComponentData) { super(engine); if (data) { this.fromData(data); } }
/** * 构造函数,只保存传入参数,不在这里创建内部对象 * @param engine - 引擎 * @param data - Mesh 参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L361-L366
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelLightComponent.onStart
override onStart (): void { this.createContent(); this.item.type = VFX_ITEM_TYPE_3D; const scene = getSceneManager(this); scene?.addItem(this.content); this.setVisible(true); }
/** * 组件开始,需要创建内部对象和添加到场景管理器中 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L371-L378
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelLightComponent.onUpdate
override onUpdate (dt: number): void { this.content.update(); }
/** * 组件更新,更新内部对象状态 * @param dt - 更新间隔 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L384-L386
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelLightComponent.onDestroy
override onDestroy (): void { this.content.dispose(); }
/** * 组件销毁 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L391-L393
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelLightComponent.fromData
override fromData (data: ModelLightComponentData): void { super.fromData(data); this.data = data; }
/** * 反序列化,记录传入参数 * @param data - 组件参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L399-L403
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelLightComponent.createContent
createContent () { if (this.data) { const lightData = this.data; this.content = new PLight(this.item.name, lightData, this); } }
/** * 创建内部对象 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L408-L414
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelCameraComponent.constructor
constructor (engine: Engine, data?: ModelCameraComponentData) { super(engine); if (data) { this.fromData(data); } }
/** * 构造函数,只保存传入参数,不在这里创建内部对象 * @param engine - 引擎 * @param data - Mesh 参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L451-L456
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelCameraComponent.onStart
override onStart (): void { this.createContent(); this.item.type = VFX_ITEM_TYPE_3D; const scene = getSceneManager(this); scene?.addItem(this.content); this.updateMainCamera(); }
/** * 组件开始,需要创建内部对象和添加到场景管理器中 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L461-L468
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelCameraComponent.onUpdate
override onUpdate (dt: number): void { this.content.update(); this.updateMainCamera(); }
/** * 组件更新,更新内部对象状态 * @param dt - 更新间隔 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L474-L477
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelCameraComponent.onDestroy
override onDestroy (): void { this.content?.dispose(); }
/** * 组件销毁 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L482-L484
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelCameraComponent.fromData
override fromData (data: ModelCameraComponentData): void { super.fromData(data); this.data = data; }
/** * 反序列化,记录传入参数 * @param data - 组件参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L490-L494
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelCameraComponent.createContent
createContent () { if (this.data) { const cameraData = this.data; const width = this.engine.renderer.getWidth(); const height = this.engine.renderer.getHeight(); this.content = new PCamera(this.item.name, width, height, cameraData, this); } }
/** * 创建内部对象 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L499-L508
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelCameraComponent.updateMainCamera
updateMainCamera () { this.content.matrix = this.transform.getWorldMatrix(); const composition = this.item.composition; if (composition) { composition.camera.position = this.transform.position.clone(); composition.camera.setQuat(this.transform.quat); composition.camera.near = this.content.nearPlane; composition.camera.far = this.content.farPlane; composition.camera.fov = this.content.fov; } }
/** * 更新合成主相机,更加当前相机元素状态 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L513-L524
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelCameraComponent.setTransform
setTransform (position?: Vector3, rotation?: Euler): void { if (position) { this.transform.setPosition(position.x, position.y, position.z); } if (rotation) { this.transform.setRotation(rotation.x, rotation.y, rotation.z); } this.updateMainCamera(); }
/** * 设置变换 * @param position - 位置 * @param rotation - 旋转 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L531-L539
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
AnimationComponent.constructor
constructor (engine: Engine) { super(engine); }
/** * 构造函数,只保存传入参数,不在这里创建内部对象 * @param engine - 引擎 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L560-L562
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
AnimationComponent.onStart
override onStart (): void { this.elapsedTime = 0; this.item.type = VFX_ITEM_TYPE_3D; }
/** * 组件开始,需要创建内部对象和添加到场景管理器中 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L567-L570
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
AnimationComponent.onUpdate
override onUpdate (dt: number): void { this.elapsedTime += dt * 0.001; if (this.animation >= 0 && this.animation < this.clips.length) { this.clips[this.animation].sampleAnimation(this.item, this.elapsedTime); } }
/** * 组件更新,更新内部对象状态 * @param dt - 更新间隔 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L576-L581
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
AnimationComponent.onDestroy
override onDestroy (): void { }
/** * 组件销毁 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L586-L588
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
AnimationComponent.fromData
override fromData (data: AnimationComponentData): void { super.fromData(data); this.data = data; // this.name = data.name ?? '<empty>'; this.animation = data.animation ?? -1; this.clips = []; data.animationClips.forEach(clipData => { const clipObj = new ModelAnimationClip(this.engine); clipObj.setFromAnimationClip(clipData as unknown as AnimationClip); this.clips.push(clipObj); }); }
/** * 反序列化,记录传入参数 * @param data - 组件参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-item.ts#L594-L607
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelPlugin.prepareResource
static override async prepareResource (scene: Scene, options: SceneLoadOptions): Promise<void> { if (options.pluginData !== undefined) { const keyList = [ 'compatibleMode', 'renderSkybox', 'visBoundingBox', 'autoAdjustScene', 'enableDynamicSort', 'renderMode3D', 'renderMode3DUVGridSize', ]; const pluginData = options.pluginData; keyList.forEach(key => scene.storage[key] = pluginData[key]); } // const runtimeEnv = options.env ?? ''; scene.storage['runtimeEnv'] = runtimeEnv; const compatibleMode = options.pluginData?.['compatibleMode'] ?? 'gltf'; // PluginHelper.preprocessScene(scene, runtimeEnv, compatibleMode); await CompositionCache.loadStaticResources(); }
/** * 整个 load 阶段都不会创建 GL 相关的对象,只创建 JS 对象 * @param scene - 场景 * @param options - 加载选项 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-plugin.ts#L38-L62
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelPlugin.onCompositionConstructed
override onCompositionConstructed (composition: Composition, scene: Scene): void { this.sceneParams = scene.storage; const engine = composition.renderer.engine; this.cache = new CompositionCache(engine); this.cache.setup(false); // FIXME: 先注释元素参数的配置 //PluginHelper.setupItem3DOptions(scene, this.cache, composition); }
/** * 创建 3D 场景管理器和缓存器 * @param composition - 合成 * @param scene - 场景 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-plugin.ts#L98-L107
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelPlugin.onCompositionReset
override onCompositionReset (composition: Composition, renderFrame: RenderFrame) { const props = { id: 'ModelPluginItem', name: 'ModelPluginItem', duration: 9999999, endBehavior: spec.END_BEHAVIOR_FORWARD, } as unknown as spec.Item; const item = new VFXItem(composition.getEngine(), props); composition.addItem(item); // const comp = item.addComponent(ModelPluginComponent); comp.fromData({ cache: this.cache }); comp.initial(this.sceneParams); }
/** * 每次播放都会执行,包括重播,所以这里执行“小的销毁”和新的初始化 * @param composition - 合成 * @param renderFrame - 渲染帧 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-plugin.ts#L114-L129
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelPlugin.onCompositionDestroyed
override onCompositionDestroyed (composition: Composition) { this.cache.dispose(); // @ts-expect-error this.cache = null; // @ts-expect-error this.sceneParams = null; }
/** * 合成销毁,同时销毁 3D 场景对象和缓存 * @param composition - 合成 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-plugin.ts#L135-L141
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelPluginComponent.constructor
constructor (engine: Engine, options?: ModelPluginOptions) { super(engine); if (options) { this.fromData(options); } }
/** * 构造函数,创建场景管理器 * @param engine - 引擎 * @param options - Mesh 参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-plugin.ts#L189-L194
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelPluginComponent.onLateUpdate
override onLateUpdate (dt: number): void { const composition = this.item.composition as Composition; if (this.autoAdjustScene && this.scene.tickCount == 1) { // 自动计算场景中的相机位置 // 更加相机的具体参数,计算出合适的相机观察位置 // 但只会在第一帧进行更新,主要是用于测试使用 const cameraObject = composition.camera; const cameraTransform = new PTransform().fromMatrix4(cameraObject.getViewMatrix()); const cameraCoordinate = new PCoordinate().fromPTransform(cameraTransform); const cameraDirection = cameraCoordinate.zAxis.clone(); const cameraFov = cameraObject.fov ?? 45; const cameraAspect = cameraObject.aspect ?? 1.0; // const sceneAABB = this.scene.getSceneAABB(); const newAABB = sceneAABB.clone().applyMatrix4(cameraTransform.getMatrix()); const newSize = newAABB.getSize(new Vector3()).multiply(0.5); const newWidth = newSize.x; const newHeight = newSize.y; const finalHeight = newHeight * Math.max(newWidth / newHeight / cameraAspect, 1.0); const center = sceneAABB.getCenter(new Vector3()); const offset = finalHeight / Math.tan(cameraFov * 0.5 * DEG2RAD); const position = center.clone().add(cameraDirection.clone().multiply(offset + newSize.z)); // 更新相机的位置,主要是composition的camera数据,以及camera item数据 composition.camera.position = position; composition.items?.forEach(item => { if (item.type === VFX_ITEM_TYPE_3D) { const component = item.getComponent(ModelCameraComponent); if (component?.content) { const worldMatrix = item.transform.parentTransform?.getWorldMatrix() || Matrix4.IDENTITY.clone(); const invWorldMatrix = worldMatrix.invert(); const newPosition = invWorldMatrix.transformPoint(position); component.setTransform(newPosition); // 正式版本不会走到这个流程,只在测试时使用 console.info(`Scene AABB [${sceneAABB.min.toArray()}], [${sceneAABB.max.toArray()}].`); console.info(`Update camera position [${newPosition.toArray()}].`); } } }); } this.updateSceneCamera(composition); this.scene.tick(dt); }
/** * 组件后更新,合成相机和场景管理器更新 * @param dt - 更新间隔 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-plugin.ts#L200-L247
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelPluginComponent.onDestroy
override onDestroy (): void { this.scene.dispose(); // @ts-expect-error this.scene = null; // @ts-expect-error this.cache = null; }
/** * 组件销毁,同时销毁场景管理器和缓存器 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-plugin.ts#L252-L258
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelPluginComponent.fromData
override fromData (data: ModelPluginOptions): void { super.fromData(data); // const options = data; this.cache = options.cache; this.scene = new PSceneManager(this.engine); }
/** * 反序列化,创建场景管理器 * @param date - 组件参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-plugin.ts#L264-L271
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelPluginComponent.initial
initial (sceneParams: Record<string, any>) { this.runtimeEnv = sceneParams['runtimeEnv'] ?? this.runtimeEnv; this.compatibleMode = sceneParams['compatibleMode'] ?? this.compatibleMode; this.renderSkybox = sceneParams['renderSkybox'] ?? this.renderSkybox; this.visBoundingBox = sceneParams['visBoundingBox'] ?? this.visBoundingBox; this.autoAdjustScene = sceneParams['autoAdjustScene'] ?? this.autoAdjustScene; this.enableDynamicSort = sceneParams['enableDynamicSort'] ?? this.enableDynamicSort; this.renderMode3D = sceneParams['renderMode3D'] ?? this.renderMode3D; this.renderMode3DUVGridSize = sceneParams['renderMode3DUVGridSize'] ?? this.renderMode3DUVGridSize; const component = this.item.composition as Composition; this.scene.initial({ componentName: `${component.id}`, renderer: this.engine.renderer, sceneCache: this.cache, runtimeEnv: this.runtimeEnv, compatibleMode: this.compatibleMode, visBoundingBox: this.visBoundingBox, enableDynamicSort: this.enableDynamicSort, renderMode3D: this.renderMode3D, renderMode3DUVGridSize: this.renderMode3DUVGridSize, renderSkybox: this.renderSkybox, lightItemCount: this.getLightItemCount(), maxJointCount: this.getMaxJointCount(), }); this.updateSceneCamera(component); }
/** * 组件初始化,初始化场景管理器并更新合成相机 * @param sceneParams - 场景参数 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-plugin.ts#L277-L304
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelPluginComponent.updateSceneCamera
private updateSceneCamera (composition: Composition) { this.scene.updateDefaultCamera(composition.camera.getOptions(), composition.camera.getViewportMatrix()); }
/** * 更新 SceneManager 中渲染用的相机,相机参数来自 composition.camera * * @param composition - 当前合成对象 * @param sceneManager - 当前合成对象绑定的 SceneManager */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-plugin.ts#L312-L314
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelTreeComponent.constructor
constructor (engine: Engine, options?: ModelTreeContent) { super(engine); if (options) { this.fromData(options); } }
/** * 构造函数,创建节点树元素 * @param engine * @param options */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-tree-component.ts#L23-L28
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
ModelTreeComponent.fromData
override fromData (options: ModelTreeContent): void { super.fromData(options); this.options = options; }
/** * 反序列化,保存入参和创建节点树元素 * @param options */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/plugin/model-tree-component.ts#L34-L37
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
PSkin.create
create (props: SkinProps, engine: Engine, rootBoneItem: VFXItem, maxJointCount: number) { this.name = props.rootBoneName ?? 'Unnamed skin'; this.type = PObjectType.skin; // this.rootBoneItem = rootBoneItem; this.skeleton = -1; this.jointItem = this.getJointItems(props, rootBoneItem); this.maxJointCount = Math.max(maxJointCount, this.jointItem.length); this.animationMatrices = []; // this.inverseBindMatrices = []; // this.textureDataMode = this.getTextureDataMode(this.maxJointCount, engine); const matList = props.inverseBindMatrices; if (matList !== undefined && matList.length > 0) { if (matList.length % 16 !== 0 || matList.length !== this.jointItem.length * 16) { throw new Error(`Invalid array length, invert bind matrices ${matList.length}, joint array ${this.jointItem.length}.`); } const matrixCount = matList.length / 16; for (let i = 0; i < matrixCount; i++) { const mat = Matrix4.fromArray(matList, i * 16); this.inverseBindMatrices.push(mat); } } }
/** * 创建蒙皮对象 * @param props - 蒙皮相关数据 * @param engine - 引擎对象 * @param rootBoneItem - 场景树父元素 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/runtime/animation.ts#L58-L86
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
PSkin.updateSkinMatrices
updateSkinMatrices () { this.animationMatrices = []; for (let i = 0; i < this.jointItem.length; i++) { const node = this.jointItem[i]; // let parent = node?.transform.parentTransform; // while(parent !== undefined){ // const pos = parent.position; // parent.setPosition(pos[0], pos[1], pos[2]); // parent = parent.parentTransform; // } const mat4 = node.transform.getWorldMatrix(); this.animationMatrices.push(mat4.clone()); } if (this.animationMatrices.length === this.inverseBindMatrices.length) { this.animationMatrices.forEach((mat, index) => { mat.multiply(this.inverseBindMatrices[index]); }); } else { this.animationMatrices = this.inverseBindMatrices; console.error('Some error occured, replace skin animation matrices by invert bind matrices.'); } }
/** * 更新蒙皮矩阵 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/runtime/animation.ts#L91-L117
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
PSkin.computeMeshAnimMatrices
computeMeshAnimMatrices (worldMatrix: Matrix4, matrixList: Float32Array, normalMatList: Float32Array) { const inverseWorldMatrix = worldMatrix.clone().invert(); const tempMatrix = new Matrix4(); this.animationMatrices.forEach((mat, i) => { const localMatrix = tempMatrix.multiplyMatrices(inverseWorldMatrix, mat); localMatrix.elements.forEach((x, j) => matrixList[i * 16 + j] = x); // const normalMat = localMatrix.clone().invert().transpose(); normalMat.elements.forEach((x, j) => normalMatList[i * 16 + j] = x); }); }
/** * 计算 Mesh 的动画矩阵 * @param worldMatrix - 世界矩阵 * @param matrixList - 矩阵列表 * @param normalMatList - 法线矩阵列表 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/runtime/animation.ts#L125-L138
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
PSkin.updateParentItem
updateParentItem (parentItem: VFXItem) { this.rootBoneItem = parentItem; }
/** * 更新父元素 * @param parentItem - 场景树父元素 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/runtime/animation.ts#L144-L146
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
PSkin.getJointCount
getJointCount (): number { return this.jointItem.length; }
/** * 获取关节点数 * @returns */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/runtime/animation.ts#L152-L154
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
PSkin.isTextureDataMode
isTextureDataMode (): boolean { return this.textureDataMode !== TextureDataMode.none; }
/** * 是否纹理数据模式 * @returns */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/runtime/animation.ts#L160-L162
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
PSkin.dispose
override dispose (): void { this.rootBoneItem = undefined; this.jointItem = []; this.inverseBindMatrices = []; this.animationMatrices = []; }
/** * 销毁 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/runtime/animation.ts#L167-L172
20512f4406e62c400b2b4255576cfa628db74a22
effects-runtime
github_2023
galacean
typescript
PMorph.create
create (geometry: Geometry): boolean { this.name = this.genName('Morph target'); this.type = PObjectType.morph; // 统计各个属性的在Morph中的数目 const positionCount = this.getAttributeMorphCount(PMorph.positionNameList, geometry); const normalCount = this.getAttributeMorphCount(PMorph.normalNameList, geometry); const tangentCount = this.getAttributeMorphCount(PMorph.tangentNameList, geometry); const countList: number[] = [positionCount, normalCount, tangentCount]; // 这里是拿到所有数目中非零的最小值,如果没有非零值,那就是直接是零 this.morphWeightsLength = 0; countList.forEach(count => { if (count > 0) { if (this.morphWeightsLength === 0) { this.morphWeightsLength = count; } else { this.morphWeightsLength = Math.min(this.morphWeightsLength, count); } } }); if (this.morphWeightsLength > 0) { // 有Morph动画,申请weights数据,判断各个属性是否有相关动画 this.morphWeightsArray = Array(this.morphWeightsLength).fill(0); this.hasPositionMorph = positionCount == this.morphWeightsLength; this.hasNormalMorph = normalCount == this.morphWeightsLength; this.hasTangentMorph = tangentCount == this.morphWeightsLength; } /** * 这里是做正确性检查,特别是各个属性之间数目需要对上,否则就报错。 * 还要注意最大数目不能超过5,否则也直接报错。 * 后续考虑是否做个兼容,目前还是严格报错比较好。 */ if (positionCount > 0 && positionCount != this.morphWeightsLength) { console.error(`Position morph count mismatch: ${this.morphWeightsLength}, ${positionCount}.`); return false; } if (normalCount > 0 && normalCount != this.morphWeightsLength) { console.error(`Normal morph count mismatch: ${this.morphWeightsLength}, ${normalCount}.`); return false; } if (tangentCount > 0 && tangentCount != this.morphWeightsLength) { console.error(`Tangent morph count mismatch: ${this.morphWeightsLength}, ${tangentCount}.`); return false; } if (this.morphWeightsLength > 5) { console.error(`Tangent morph count should not greater than 5, current ${this.morphWeightsLength}.`); return false; } return true; }
/** * 通过 Geometry 数据创建 Morph 动画相关状态,并进行必要的正确性检查 * @param geometry - Mesh 的几何体,是否包含 Morph 动画都是可以的 * @returns 是否创建成功 */
https://github.com/galacean/effects-runtime/blob/20512f4406e62c400b2b4255576cfa628db74a22/plugin-packages/model/src/runtime/animation.ts#L268-L329
20512f4406e62c400b2b4255576cfa628db74a22