@planara/core 1.4.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,9 @@ export declare class EditorRenderer extends Renderer {
12
12
  /** Raycast для подсветки моделей при наведении */
13
13
  private raycast;
14
14
  /** Курсор мыши для остлеживания наведения на 3D-модель */
15
- private readonly mouse;
15
+ private mouse;
16
+ /** Были ли зарегистрированы обработчики событий для мыши */
17
+ private isEventListenersAdded;
16
18
  /**
17
19
  * Инициализация сцены редактора.
18
20
  * Создает сетку, оси координат и orbit-контроллер.
@@ -41,5 +43,7 @@ export declare class EditorRenderer extends Renderer {
41
43
  * Обработчик движения мыши
42
44
  */
43
45
  private handleMouseMove;
46
+ /** Деструктор */
47
+ destroy(): void;
44
48
  }
45
49
  //# sourceMappingURL=editor-renderer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"editor-renderer.d.ts","sourceRoot":"","sources":["../../src/core/editor-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAgD,KAAK,IAAI,EAAE,MAAM,KAAK,CAAC;AAC9E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;;GAIG;AACH,qBAAa,cAAe,SAAQ,QAAQ;IAC1C,8CAA8C;IAC9C,OAAO,CAAC,KAAK,CAAS;IAEtB,kDAAkD;IAClD,OAAO,CAAC,OAAO,CAAW;IAE1B,0DAA0D;IAC1D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAQ;IAE9B;;;;OAIG;gBACgB,MAAM,EAAE,iBAAiB;IAyB5C;;OAEG;IACH,SAAS,CAAC,MAAM;IAKhB;;;;OAIG;IACa,SAAS,CAAC,MAAM,EAAE,MAAM;IAsBxC;;OAEG;IACH,SAAS,CAAC,gBAAgB,CAAC,IAAI,EAAE,IAAI;IAIrC;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAO1B;;OAEG;IACH,OAAO,CAAC,eAAe,CA2BrB;CACH"}
1
+ {"version":3,"file":"editor-renderer.d.ts","sourceRoot":"","sources":["../../src/core/editor-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAgD,KAAK,IAAI,EAAE,MAAM,KAAK,CAAC;AAC9E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;;GAIG;AACH,qBAAa,cAAe,SAAQ,QAAQ;IAC1C,8CAA8C;IAC9C,OAAO,CAAC,KAAK,CAAS;IAEtB,kDAAkD;IAClD,OAAO,CAAC,OAAO,CAAW;IAE1B,0DAA0D;IAC1D,OAAO,CAAC,KAAK,CAAQ;IAErB,4DAA4D;IAC5D,OAAO,CAAC,qBAAqB,CAAW;IAExC;;;;OAIG;gBACgB,MAAM,EAAE,iBAAiB;IAyB5C;;OAEG;IACH,SAAS,CAAC,MAAM;IAKhB;;;;OAIG;IACa,SAAS,CAAC,MAAM,EAAE,MAAM;IA4BxC;;OAEG;IACH,SAAS,CAAC,gBAAgB,CAAC,IAAI,EAAE,IAAI;IAIrC;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAM1B;;OAEG;IACH,OAAO,CAAC,eAAe,CAerB;IAEF,iBAAiB;IACV,OAAO;CAaf"}
@@ -43,5 +43,7 @@ export declare abstract class Renderer {
43
43
  * @param figure Данные фигуры: position, normal, uv
44
44
  */
45
45
  addFigure(figure: Figure): Mesh<Geometry, Program>;
46
+ /** Деструктор */
47
+ destroy(): void;
46
48
  }
47
49
  //# sourceMappingURL=renderer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"renderer.d.ts","sourceRoot":"","sources":["../../src/core/renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,WAAW,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,OAAO,EAAE,MAAM,KAAK,CAAC;AAE/F,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;GAGG;AACH,8BAAsB,QAAQ;IAC5B,8BAA8B;IAC9B,SAAS,CAAC,EAAE,EAAE,WAAW,CAAC;IAE1B,4BAA4B;IAC5B,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC;IAE3B,uBAAuB;IACvB,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAEzB,uDAAuD;IACvD,SAAS,CAAC,MAAM,EAAE,iBAAiB,CAAC;IAEpC,+CAA+C;IAC/C,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC;IAE3B,8BAA8B;IAC9B,SAAS,CAAC,MAAM,EAAG,IAAI,EAAE,CAAC;IAE1B;;;OAGG;IACH,SAAS,aAAa,MAAM,EAAE,iBAAiB;IAyB/C;;OAEG;IACI,MAAM;IAMb;;OAEG;IACH,SAAS,CAAC,MAAM;IAIhB;;OAEG;IACH,SAAS,CAAC,MAAM,IAAI,IAAI;IAExB;;OAEG;IACI,IAAI;IAMX;;;OAGG;IACI,SAAS,CAAC,MAAM,EAAE,MAAM;CAqBhC"}
1
+ {"version":3,"file":"renderer.d.ts","sourceRoot":"","sources":["../../src/core/renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,WAAW,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,OAAO,EAAE,MAAM,KAAK,CAAC;AAE/F,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;GAGG;AACH,8BAAsB,QAAQ;IAC5B,8BAA8B;IAC9B,SAAS,CAAC,EAAE,EAAE,WAAW,CAAC;IAE1B,4BAA4B;IAC5B,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC;IAE3B,uBAAuB;IACvB,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAEzB,uDAAuD;IACvD,SAAS,CAAC,MAAM,EAAE,iBAAiB,CAAC;IAEpC,+CAA+C;IAC/C,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC;IAE3B,8BAA8B;IAC9B,SAAS,CAAC,MAAM,EAAG,IAAI,EAAE,CAAC;IAE1B;;;OAGG;IACH,SAAS,aAAa,MAAM,EAAE,iBAAiB;IAyB/C;;OAEG;IACI,MAAM;IAMb;;OAEG;IACH,SAAS,CAAC,MAAM;IAIhB;;OAEG;IACH,SAAS,CAAC,MAAM,IAAI,IAAI;IAExB;;OAEG;IACI,IAAI;IAMX;;;OAGG;IACI,SAAS,CAAC,MAAM,EAAE,MAAM;IAsB/B,iBAAiB;IACV,OAAO;CAef"}
package/dist/index.cjs.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("ogl");function v(s){const e=`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("ogl");function d(s){const e=`
2
2
  attribute vec3 position;
3
3
  attribute vec3 normal;
4
4
  attribute vec2 uv;
@@ -29,5 +29,5 @@
29
29
  gl_FragColor.rgb = tex + shading;
30
30
  gl_FragColor.a = 1.0;
31
31
  }
32
- `,i=new r.Texture(s);return new r.Program(s,{vertex:e,fragment:t,uniforms:{tMap:{value:i}}})}class h{gl;scene;camera;canvas;program;meshes;constructor(e){this.canvas=e,this.gl=new r.Renderer({canvas:e}),this.gl.setSize(e.clientWidth,e.clientHeight),this.gl.gl.clearColor(1,1,1,1),this.scene=new r.Transform,this.camera=new r.Camera(this.gl.gl,{fov:45}),this.camera.position.set(1,1,7),this.camera.lookAt([0,0,0]),this.program=v(this.gl.gl)}resize(){this.gl.setSize(this.canvas.width,this.canvas.height),this.camera.perspective({aspect:this.canvas.width/this.canvas.height})}render(){this.gl.render({scene:this.scene,camera:this.camera})}update(){}loop(){this.update(),this.render(),requestAnimationFrame(this.loop.bind(this))}addFigure(e){const t=new r.Geometry(this.gl.gl,{position:{size:3,data:new Float32Array(e.position)},normal:{size:3,data:new Float32Array(e.normal??[])},uv:{size:2,data:new Float32Array(e.uv??[])}}),i=new r.Mesh(this.gl.gl,{geometry:t,program:this.program});return i.setParent(this.scene),this.meshes.push(i),i}}class p extends h{orbit;raycast;mouse;constructor(e){super(e);const t=new r.GridHelper(this.gl.gl,{size:10,divisions:10});t.position.y=-.001,t.setParent(this.scene),new r.AxesHelper(this.gl.gl,{size:6,symmetric:!0}).setParent(this.scene),this.orbit=new r.Orbit(this.camera,{element:this.canvas}),this.raycast=new r.Raycast,this.mouse=new r.Vec2,this.initMouseListeners()}update(){this.orbit?.update()}addFigure(e){const t=super.addFigure(e);if(t.geometry){const i=t.geometry.constructor.name;t.geometry.raycast=i.includes("Sphere")?"sphere":"box"}return t.isHit=!1,t.onBeforeRender(({mesh:i})=>{this.updateHitUniform(i)}),t}updateHitUniform(e){this.program.uniforms.uHit.value=e.isHit?1:0}initMouseListeners(){window.addEventListener("load",()=>{document.addEventListener("mousemove",this.handleMouseMove,!1),document.addEventListener("touchmove",this.handleMouseMove,!1)})}handleMouseMove=e=>{let t,i;if(e instanceof MouseEvent)t=e.clientX,i=e.clientY;else{if(e.touches[0]===void 0)return;t=e.touches[0].clientX,i=e.touches[0].clientY}this.mouse.set(2*(t/this.canvas.width)-1,2*(1-i/this.canvas.height)-1),this.raycast.castMouse(this.camera,this.mouse),this.meshes.forEach(a=>a.isHit=!1),this.raycast.intersectBounds(this.meshes).forEach(a=>a.isHit=!0)}}class g extends h{orbit;constructor(e){super(e),this.orbit=new r.Orbit(this.camera,{element:this.canvas,target:new r.Vec3(0,0,0),minPolarAngle:Math.PI/2,maxPolarAngle:Math.PI/2,enableRotate:!0,enableZoom:!1,enablePan:!1})}update(){this.orbit?.update()}}class f{type;position;normal;uv;material;constructor(e){this.type=e.type,this.position=e.position,this.normal=e.normal??[],this.uv=e.uv??[],this.material=e.material}}var d=(s=>(s[s.Cube=0]="Cube",s[s.Sphere=1]="Sphere",s[s.Plane=2]="Plane",s[s.Cylinder=3]="Cylinder",s[s.Custom=4]="Custom",s))(d||{});class b{positions=[];normals=[];uvs=[];tmpPositions=[];tmpNormals=[];tmpUVs=[];load(e){const t=e.split(`
33
- `);for(const n of t){if(!n.trim()||n.startsWith("#"))continue;const a=n.trim().split(/\s+/);switch(a[0]){case"v":this.tmpPositions.push(a.slice(1).map(Number));break;case"vn":this.tmpNormals.push(a.slice(1).map(Number));break;case"vt":this.tmpUVs.push(a.slice(1).map(Number));break;case"f":this.processFaceLine(a);break}}const i={type:d.Custom,position:this.positions,...this.normals.length>0&&{normal:this.normals},...this.uvs.length>0&&{uv:this.uvs}};return new f(i)}processFaceLine(e){for(let t=1;t<e.length;t++){const i=e[t];if(!i)continue;const[n,a,c]=i.split("/"),l=n?parseInt(n,10):void 0,m=a?parseInt(a,10):void 0,u=c?parseInt(c,10):void 0;if(l!==void 0){const o=this.tmpPositions[l-1];o&&this.positions.push(...o)}if(m!==void 0){const o=this.tmpUVs[m-1];o&&this.uvs.push(...o)}if(u!==void 0){const o=this.tmpNormals[u-1];o&&this.normals.push(...o)}}}}exports.EditorRenderer=p;exports.ObjLoader=b;exports.PreviewRenderer=g;exports.Renderer=h;exports.createProgram=v;
32
+ `,i=new r.Texture(s);return new r.Program(s,{vertex:e,fragment:t,uniforms:{tMap:{value:i}}})}class h{gl;scene;camera;canvas;program;meshes;constructor(e){this.canvas=e,this.gl=new r.Renderer({canvas:e}),this.gl.setSize(e.clientWidth,e.clientHeight),this.gl.gl.clearColor(1,1,1,1),this.scene=new r.Transform,this.camera=new r.Camera(this.gl.gl,{fov:45}),this.camera.position.set(1,1,7),this.camera.lookAt([0,0,0]),this.program=d(this.gl.gl)}resize(){this.gl.setSize(this.canvas.width,this.canvas.height),this.camera.perspective({aspect:this.canvas.width/this.canvas.height})}render(){this.gl.render({scene:this.scene,camera:this.camera})}update(){}loop(){this.update(),this.render(),requestAnimationFrame(this.loop.bind(this))}addFigure(e){const t=new r.Geometry(this.gl.gl,{position:{size:3,data:new Float32Array(e.position)},normal:{size:3,data:new Float32Array(e.normal??[])},uv:{size:2,data:new Float32Array(e.uv??[])}}),i=new r.Mesh(this.gl.gl,{geometry:t,program:this.program});return i.setParent(this.scene),this.meshes.push(i),i}destroy(){this.meshes&&(this.meshes.length=0,this.meshes=[]),this.scene=null,this.camera=null,this.program=null,this.gl=null,this.canvas=null}}class p extends h{orbit;raycast;mouse;isEventListenersAdded;constructor(e){super(e);const t=new r.GridHelper(this.gl.gl,{size:10,divisions:10});t.position.y=-.001,t.setParent(this.scene),new r.AxesHelper(this.gl.gl,{size:6,symmetric:!0}).setParent(this.scene),this.orbit=new r.Orbit(this.camera,{element:this.canvas}),this.raycast=new r.Raycast,this.mouse=new r.Vec2,this.isEventListenersAdded=!1}update(){this.orbit?.update()}addFigure(e){const t=super.addFigure(e);if(t.geometry){const i=t.geometry.constructor.name;t.geometry.raycast=i.includes("Sphere")?"sphere":"box"}return t.isHit=!1,t.onBeforeRender(({mesh:i})=>{this.updateHitUniform(i)}),this.isEventListenersAdded&&(this.initMouseListeners(),this.isEventListenersAdded=!0),t}updateHitUniform(e){this.program.uniforms.uHit.value=e.isHit?1:0}initMouseListeners(){window.addEventListener("load",()=>{document.addEventListener("mousemove",this.handleMouseMove,!1)})}handleMouseMove=e=>{this.mouse.set(2*(e.x/this.gl.width)-1,2*(1-e.y/this.gl.height)-1),this.raycast.castMouse(this.camera,this.mouse),this.meshes.forEach(i=>i.isHit=!1),this.raycast.intersectBounds(this.meshes).forEach(i=>i.isHit=!0)};destroy(){this.isEventListenersAdded&&(window.removeEventListener("mousemove",this.handleMouseMove,!1),this.isEventListenersAdded=!1),this.orbit=null,this.raycast=null,this.mouse=null,super.destroy()}}class g extends h{orbit;constructor(e){super(e),this.orbit=new r.Orbit(this.camera,{element:this.canvas,target:new r.Vec3(0,0,0),minPolarAngle:Math.PI/2,maxPolarAngle:Math.PI/2,enableRotate:!0,enableZoom:!1,enablePan:!1})}update(){this.orbit?.update()}}class f{type;position;normal;uv;material;constructor(e){this.type=e.type,this.position=e.position,this.normal=e.normal??[],this.uv=e.uv??[],this.material=e.material}}var v=(s=>(s[s.Cube=0]="Cube",s[s.Sphere=1]="Sphere",s[s.Plane=2]="Plane",s[s.Cylinder=3]="Cylinder",s[s.Custom=4]="Custom",s))(v||{});class y{positions=[];normals=[];uvs=[];tmpPositions=[];tmpNormals=[];tmpUVs=[];load(e){const t=e.split(`
33
+ `);for(const o of t){if(!o.trim()||o.startsWith("#"))continue;const n=o.trim().split(/\s+/);switch(n[0]){case"v":this.tmpPositions.push(n.slice(1).map(Number));break;case"vn":this.tmpNormals.push(n.slice(1).map(Number));break;case"vt":this.tmpUVs.push(n.slice(1).map(Number));break;case"f":this.processFaceLine(n);break}}const i={type:v.Custom,position:this.positions,...this.normals.length>0&&{normal:this.normals},...this.uvs.length>0&&{uv:this.uvs}};return new f(i)}processFaceLine(e){for(let t=1;t<e.length;t++){const i=e[t];if(!i)continue;const[o,n,l]=i.split("/"),m=o?parseInt(o,10):void 0,c=n?parseInt(n,10):void 0,u=l?parseInt(l,10):void 0;if(m!==void 0){const a=this.tmpPositions[m-1];a&&this.positions.push(...a)}if(c!==void 0){const a=this.tmpUVs[c-1];a&&this.uvs.push(...a)}if(u!==void 0){const a=this.tmpNormals[u-1];a&&this.normals.push(...a)}}}}exports.EditorRenderer=p;exports.ObjLoader=y;exports.PreviewRenderer=g;exports.Renderer=h;exports.createProgram=d;
package/dist/index.es.js CHANGED
@@ -1,5 +1,5 @@
1
- import { Texture as p, Program as d, Renderer as g, Transform as f, Camera as b, Geometry as w, Mesh as x, GridHelper as y, AxesHelper as M, Orbit as m, Raycast as P, Vec2 as z, Vec3 as C } from "ogl";
2
- function F(s) {
1
+ import { Texture as v, Program as p, Renderer as g, Transform as f, Camera as y, Geometry as w, Mesh as b, GridHelper as x, AxesHelper as M, Orbit as c, Raycast as P, Vec2 as A, Vec3 as L } from "ogl";
2
+ function E(s) {
3
3
  const e = (
4
4
  /* glsl */
5
5
  `
@@ -37,8 +37,8 @@ function F(s) {
37
37
  gl_FragColor.a = 1.0;
38
38
  }
39
39
  `
40
- ), i = new p(s);
41
- return new d(s, {
40
+ ), i = new v(s);
41
+ return new p(s, {
42
42
  vertex: e,
43
43
  fragment: t,
44
44
  uniforms: { tMap: { value: i } }
@@ -62,7 +62,7 @@ class u {
62
62
  * @param canvas - HTMLCanvasElement для рендеринга
63
63
  */
64
64
  constructor(e) {
65
- this.canvas = e, this.gl = new g({ canvas: e }), this.gl.setSize(e.clientWidth, e.clientHeight), this.gl.gl.clearColor(1, 1, 1, 1), this.scene = new f(), this.camera = new b(this.gl.gl, { fov: 45 }), this.camera.position.set(1, 1, 7), this.camera.lookAt([0, 0, 0]), this.program = F(this.gl.gl);
65
+ this.canvas = e, this.gl = new g({ canvas: e }), this.gl.setSize(e.clientWidth, e.clientHeight), this.gl.gl.clearColor(1, 1, 1, 1), this.scene = new f(), this.camera = new y(this.gl.gl, { fov: 45 }), this.camera.position.set(1, 1, 7), this.camera.lookAt([0, 0, 0]), this.program = E(this.gl.gl);
66
66
  }
67
67
  /**
68
68
  * Обновляет размер рендерера и камеры при изменении размеров canvas.
@@ -96,20 +96,26 @@ class u {
96
96
  position: { size: 3, data: new Float32Array(e.position) },
97
97
  normal: { size: 3, data: new Float32Array(e.normal ?? []) },
98
98
  uv: { size: 2, data: new Float32Array(e.uv ?? []) }
99
- }), i = new x(this.gl.gl, {
99
+ }), i = new b(this.gl.gl, {
100
100
  geometry: t,
101
101
  program: this.program
102
102
  });
103
103
  return i.setParent(this.scene), this.meshes.push(i), i;
104
104
  }
105
+ /** Деструктор */
106
+ destroy() {
107
+ this.meshes && (this.meshes.length = 0, this.meshes = []), this.scene = null, this.camera = null, this.program = null, this.gl = null, this.canvas = null;
108
+ }
105
109
  }
106
- class N extends u {
110
+ class F extends u {
107
111
  /** Orbit-контроллер для управления камерой */
108
112
  orbit;
109
113
  /** Raycast для подсветки моделей при наведении */
110
114
  raycast;
111
115
  /** Курсор мыши для остлеживания наведения на 3D-модель */
112
116
  mouse;
117
+ /** Были ли зарегистрированы обработчики событий для мыши */
118
+ isEventListenersAdded;
113
119
  /**
114
120
  * Инициализация сцены редактора.
115
121
  * Создает сетку, оси координат и orbit-контроллер.
@@ -117,8 +123,8 @@ class N extends u {
117
123
  */
118
124
  constructor(e) {
119
125
  super(e);
120
- const t = new y(this.gl.gl, { size: 10, divisions: 10 });
121
- t.position.y = -1e-3, t.setParent(this.scene), new M(this.gl.gl, { size: 6, symmetric: !0 }).setParent(this.scene), this.orbit = new m(this.camera, { element: this.canvas }), this.raycast = new P(), this.mouse = new z(), this.initMouseListeners();
126
+ const t = new x(this.gl.gl, { size: 10, divisions: 10 });
127
+ t.position.y = -1e-3, t.setParent(this.scene), new M(this.gl.gl, { size: 6, symmetric: !0 }).setParent(this.scene), this.orbit = new c(this.camera, { element: this.canvas }), this.raycast = new P(), this.mouse = new A(), this.isEventListenersAdded = !1;
122
128
  }
123
129
  /**
124
130
  * Обновление состояния рендерера.
@@ -139,7 +145,7 @@ class N extends u {
139
145
  }
140
146
  return t.isHit = !1, t.onBeforeRender(({ mesh: i }) => {
141
147
  this.updateHitUniform(i);
142
- }), t;
148
+ }), this.isEventListenersAdded && (this.initMouseListeners(), this.isEventListenersAdded = !0), t;
143
149
  }
144
150
  /**
145
151
  * Обновление uniform uHit для конкретной 3D-модели
@@ -152,24 +158,21 @@ class N extends u {
152
158
  */
153
159
  initMouseListeners() {
154
160
  window.addEventListener("load", () => {
155
- document.addEventListener("mousemove", this.handleMouseMove, !1), document.addEventListener("touchmove", this.handleMouseMove, !1);
161
+ document.addEventListener("mousemove", this.handleMouseMove, !1);
156
162
  });
157
163
  }
158
164
  /**
159
165
  * Обработчик движения мыши
160
166
  */
161
167
  handleMouseMove = (e) => {
162
- let t, i;
163
- if (e instanceof MouseEvent)
164
- t = e.clientX, i = e.clientY;
165
- else {
166
- if (e.touches[0] === void 0) return;
167
- t = e.touches[0].clientX, i = e.touches[0].clientY;
168
- }
169
- this.mouse.set(2 * (t / this.canvas.width) - 1, 2 * (1 - i / this.canvas.height) - 1), this.raycast.castMouse(this.camera, this.mouse), this.meshes.forEach((r) => r.isHit = !1), this.raycast.intersectBounds(this.meshes).forEach((r) => r.isHit = !0);
168
+ this.mouse.set(2 * (e.x / this.gl.width) - 1, 2 * (1 - e.y / this.gl.height) - 1), this.raycast.castMouse(this.camera, this.mouse), this.meshes.forEach((i) => i.isHit = !1), this.raycast.intersectBounds(this.meshes).forEach((i) => i.isHit = !0);
170
169
  };
170
+ /** Деструктор */
171
+ destroy() {
172
+ this.isEventListenersAdded && (window.removeEventListener("mousemove", this.handleMouseMove, !1), this.isEventListenersAdded = !1), this.orbit = null, this.raycast = null, this.mouse = null, super.destroy();
173
+ }
171
174
  }
172
- class U extends u {
175
+ class I extends u {
173
176
  /** Orbit-контроллер для управления камерой */
174
177
  orbit;
175
178
  /**
@@ -177,9 +180,9 @@ class U extends u {
177
180
  * @param canvas - HTMLCanvasElement для рендеринга
178
181
  */
179
182
  constructor(e) {
180
- super(e), this.orbit = new m(this.camera, {
183
+ super(e), this.orbit = new c(this.camera, {
181
184
  element: this.canvas,
182
- target: new C(0, 0, 0),
185
+ target: new L(0, 0, 0),
183
186
  minPolarAngle: Math.PI / 2,
184
187
  maxPolarAngle: Math.PI / 2,
185
188
  enableRotate: !0,
@@ -194,7 +197,7 @@ class U extends u {
194
197
  this.orbit?.update();
195
198
  }
196
199
  }
197
- class I {
200
+ class z {
198
201
  /** Тип фигуры */
199
202
  type;
200
203
  /** Позиции вершин */
@@ -213,8 +216,8 @@ class I {
213
216
  this.type = e.type, this.position = e.position, this.normal = e.normal ?? [], this.uv = e.uv ?? [], this.material = e.material;
214
217
  }
215
218
  }
216
- var v = /* @__PURE__ */ ((s) => (s[s.Cube = 0] = "Cube", s[s.Sphere = 1] = "Sphere", s[s.Plane = 2] = "Plane", s[s.Cylinder = 3] = "Cylinder", s[s.Custom = 4] = "Custom", s))(v || {});
217
- class A {
219
+ var d = /* @__PURE__ */ ((s) => (s[s.Cube = 0] = "Cube", s[s.Sphere = 1] = "Sphere", s[s.Plane = 2] = "Plane", s[s.Cylinder = 3] = "Cylinder", s[s.Custom = 4] = "Custom", s))(d || {});
220
+ class H {
218
221
  /** Позиции вершин */
219
222
  positions = [];
220
223
  /** Нормали вершин */
@@ -232,9 +235,9 @@ class A {
232
235
  load(e) {
233
236
  const t = e.split(`
234
237
  `);
235
- for (const o of t) {
236
- if (!o.trim() || o.startsWith("#")) continue;
237
- const r = o.trim().split(/\s+/);
238
+ for (const n of t) {
239
+ if (!n.trim() || n.startsWith("#")) continue;
240
+ const r = n.trim().split(/\s+/);
238
241
  switch (r[0]) {
239
242
  case "v":
240
243
  this.tmpPositions.push(r.slice(1).map(Number));
@@ -251,12 +254,12 @@ class A {
251
254
  }
252
255
  }
253
256
  const i = {
254
- type: v.Custom,
257
+ type: d.Custom,
255
258
  position: this.positions,
256
259
  ...this.normals.length > 0 && { normal: this.normals },
257
260
  ...this.uvs.length > 0 && { uv: this.uvs }
258
261
  };
259
- return new I(i);
262
+ return new z(i);
260
263
  }
261
264
  /**
262
265
  * Обрабатывает строку face (f) и разворачивает индексы в массивы для рендеринга
@@ -265,26 +268,26 @@ class A {
265
268
  for (let t = 1; t < e.length; t++) {
266
269
  const i = e[t];
267
270
  if (!i) continue;
268
- const [o, r, n] = i.split("/"), h = o ? parseInt(o, 10) : void 0, c = r ? parseInt(r, 10) : void 0, l = n ? parseInt(n, 10) : void 0;
271
+ const [n, r, o] = i.split("/"), h = n ? parseInt(n, 10) : void 0, l = r ? parseInt(r, 10) : void 0, m = o ? parseInt(o, 10) : void 0;
269
272
  if (h !== void 0) {
270
273
  const a = this.tmpPositions[h - 1];
271
274
  a && this.positions.push(...a);
272
275
  }
273
- if (c !== void 0) {
274
- const a = this.tmpUVs[c - 1];
276
+ if (l !== void 0) {
277
+ const a = this.tmpUVs[l - 1];
275
278
  a && this.uvs.push(...a);
276
279
  }
277
- if (l !== void 0) {
278
- const a = this.tmpNormals[l - 1];
280
+ if (m !== void 0) {
281
+ const a = this.tmpNormals[m - 1];
279
282
  a && this.normals.push(...a);
280
283
  }
281
284
  }
282
285
  }
283
286
  }
284
287
  export {
285
- N as EditorRenderer,
286
- A as ObjLoader,
287
- U as PreviewRenderer,
288
+ F as EditorRenderer,
289
+ H as ObjLoader,
290
+ I as PreviewRenderer,
288
291
  u as Renderer,
289
- F as createProgram
292
+ E as createProgram
290
293
  };
package/dist/index.umd.js CHANGED
@@ -1,4 +1,4 @@
1
- (function(a,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("ogl")):typeof define=="function"&&define.amd?define(["exports","ogl"],r):(a=typeof globalThis<"u"?globalThis:a||self,r(a.PlanaraCore={},a.OGL))})(this,(function(a,r){"use strict";function m(s){const e=`
1
+ (function(n,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("ogl")):typeof define=="function"&&define.amd?define(["exports","ogl"],r):(n=typeof globalThis<"u"?globalThis:n||self,r(n.PlanaraCore={},n.OGL))})(this,(function(n,r){"use strict";function m(s){const e=`
2
2
  attribute vec3 position;
3
3
  attribute vec3 normal;
4
4
  attribute vec2 uv;
@@ -29,5 +29,5 @@
29
29
  gl_FragColor.rgb = tex + shading;
30
30
  gl_FragColor.a = 1.0;
31
31
  }
32
- `,i=new r.Texture(s);return new r.Program(s,{vertex:e,fragment:t,uniforms:{tMap:{value:i}}})}class c{gl;scene;camera;canvas;program;meshes;constructor(e){this.canvas=e,this.gl=new r.Renderer({canvas:e}),this.gl.setSize(e.clientWidth,e.clientHeight),this.gl.gl.clearColor(1,1,1,1),this.scene=new r.Transform,this.camera=new r.Camera(this.gl.gl,{fov:45}),this.camera.position.set(1,1,7),this.camera.lookAt([0,0,0]),this.program=m(this.gl.gl)}resize(){this.gl.setSize(this.canvas.width,this.canvas.height),this.camera.perspective({aspect:this.canvas.width/this.canvas.height})}render(){this.gl.render({scene:this.scene,camera:this.camera})}update(){}loop(){this.update(),this.render(),requestAnimationFrame(this.loop.bind(this))}addFigure(e){const t=new r.Geometry(this.gl.gl,{position:{size:3,data:new Float32Array(e.position)},normal:{size:3,data:new Float32Array(e.normal??[])},uv:{size:2,data:new Float32Array(e.uv??[])}}),i=new r.Mesh(this.gl.gl,{geometry:t,program:this.program});return i.setParent(this.scene),this.meshes.push(i),i}}class f extends c{orbit;raycast;mouse;constructor(e){super(e);const t=new r.GridHelper(this.gl.gl,{size:10,divisions:10});t.position.y=-.001,t.setParent(this.scene),new r.AxesHelper(this.gl.gl,{size:6,symmetric:!0}).setParent(this.scene),this.orbit=new r.Orbit(this.camera,{element:this.canvas}),this.raycast=new r.Raycast,this.mouse=new r.Vec2,this.initMouseListeners()}update(){this.orbit?.update()}addFigure(e){const t=super.addFigure(e);if(t.geometry){const i=t.geometry.constructor.name;t.geometry.raycast=i.includes("Sphere")?"sphere":"box"}return t.isHit=!1,t.onBeforeRender(({mesh:i})=>{this.updateHitUniform(i)}),t}updateHitUniform(e){this.program.uniforms.uHit.value=e.isHit?1:0}initMouseListeners(){window.addEventListener("load",()=>{document.addEventListener("mousemove",this.handleMouseMove,!1),document.addEventListener("touchmove",this.handleMouseMove,!1)})}handleMouseMove=e=>{let t,i;if(e instanceof MouseEvent)t=e.clientX,i=e.clientY;else{if(e.touches[0]===void 0)return;t=e.touches[0].clientX,i=e.touches[0].clientY}this.mouse.set(2*(t/this.canvas.width)-1,2*(1-i/this.canvas.height)-1),this.raycast.castMouse(this.camera,this.mouse),this.meshes.forEach(n=>n.isHit=!1),this.raycast.intersectBounds(this.meshes).forEach(n=>n.isHit=!0)}}class g extends c{orbit;constructor(e){super(e),this.orbit=new r.Orbit(this.camera,{element:this.canvas,target:new r.Vec3(0,0,0),minPolarAngle:Math.PI/2,maxPolarAngle:Math.PI/2,enableRotate:!0,enableZoom:!1,enablePan:!1})}update(){this.orbit?.update()}}class b{type;position;normal;uv;material;constructor(e){this.type=e.type,this.position=e.position,this.normal=e.normal??[],this.uv=e.uv??[],this.material=e.material}}var l=(s=>(s[s.Cube=0]="Cube",s[s.Sphere=1]="Sphere",s[s.Plane=2]="Plane",s[s.Cylinder=3]="Cylinder",s[s.Custom=4]="Custom",s))(l||{});class y{positions=[];normals=[];uvs=[];tmpPositions=[];tmpNormals=[];tmpUVs=[];load(e){const t=e.split(`
33
- `);for(const h of t){if(!h.trim()||h.startsWith("#"))continue;const n=h.trim().split(/\s+/);switch(n[0]){case"v":this.tmpPositions.push(n.slice(1).map(Number));break;case"vn":this.tmpNormals.push(n.slice(1).map(Number));break;case"vt":this.tmpUVs.push(n.slice(1).map(Number));break;case"f":this.processFaceLine(n);break}}const i={type:l.Custom,position:this.positions,...this.normals.length>0&&{normal:this.normals},...this.uvs.length>0&&{uv:this.uvs}};return new b(i)}processFaceLine(e){for(let t=1;t<e.length;t++){const i=e[t];if(!i)continue;const[h,n,u]=i.split("/"),d=h?parseInt(h,10):void 0,v=n?parseInt(n,10):void 0,p=u?parseInt(u,10):void 0;if(d!==void 0){const o=this.tmpPositions[d-1];o&&this.positions.push(...o)}if(v!==void 0){const o=this.tmpUVs[v-1];o&&this.uvs.push(...o)}if(p!==void 0){const o=this.tmpNormals[p-1];o&&this.normals.push(...o)}}}}a.EditorRenderer=f,a.ObjLoader=y,a.PreviewRenderer=g,a.Renderer=c,a.createProgram=m,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})}));
32
+ `,i=new r.Texture(s);return new r.Program(s,{vertex:e,fragment:t,uniforms:{tMap:{value:i}}})}class l{gl;scene;camera;canvas;program;meshes;constructor(e){this.canvas=e,this.gl=new r.Renderer({canvas:e}),this.gl.setSize(e.clientWidth,e.clientHeight),this.gl.gl.clearColor(1,1,1,1),this.scene=new r.Transform,this.camera=new r.Camera(this.gl.gl,{fov:45}),this.camera.position.set(1,1,7),this.camera.lookAt([0,0,0]),this.program=m(this.gl.gl)}resize(){this.gl.setSize(this.canvas.width,this.canvas.height),this.camera.perspective({aspect:this.canvas.width/this.canvas.height})}render(){this.gl.render({scene:this.scene,camera:this.camera})}update(){}loop(){this.update(),this.render(),requestAnimationFrame(this.loop.bind(this))}addFigure(e){const t=new r.Geometry(this.gl.gl,{position:{size:3,data:new Float32Array(e.position)},normal:{size:3,data:new Float32Array(e.normal??[])},uv:{size:2,data:new Float32Array(e.uv??[])}}),i=new r.Mesh(this.gl.gl,{geometry:t,program:this.program});return i.setParent(this.scene),this.meshes.push(i),i}destroy(){this.meshes&&(this.meshes.length=0,this.meshes=[]),this.scene=null,this.camera=null,this.program=null,this.gl=null,this.canvas=null}}class g extends l{orbit;raycast;mouse;isEventListenersAdded;constructor(e){super(e);const t=new r.GridHelper(this.gl.gl,{size:10,divisions:10});t.position.y=-.001,t.setParent(this.scene),new r.AxesHelper(this.gl.gl,{size:6,symmetric:!0}).setParent(this.scene),this.orbit=new r.Orbit(this.camera,{element:this.canvas}),this.raycast=new r.Raycast,this.mouse=new r.Vec2,this.isEventListenersAdded=!1}update(){this.orbit?.update()}addFigure(e){const t=super.addFigure(e);if(t.geometry){const i=t.geometry.constructor.name;t.geometry.raycast=i.includes("Sphere")?"sphere":"box"}return t.isHit=!1,t.onBeforeRender(({mesh:i})=>{this.updateHitUniform(i)}),this.isEventListenersAdded&&(this.initMouseListeners(),this.isEventListenersAdded=!0),t}updateHitUniform(e){this.program.uniforms.uHit.value=e.isHit?1:0}initMouseListeners(){window.addEventListener("load",()=>{document.addEventListener("mousemove",this.handleMouseMove,!1)})}handleMouseMove=e=>{this.mouse.set(2*(e.x/this.gl.width)-1,2*(1-e.y/this.gl.height)-1),this.raycast.castMouse(this.camera,this.mouse),this.meshes.forEach(i=>i.isHit=!1),this.raycast.intersectBounds(this.meshes).forEach(i=>i.isHit=!0)};destroy(){this.isEventListenersAdded&&(window.removeEventListener("mousemove",this.handleMouseMove,!1),this.isEventListenersAdded=!1),this.orbit=null,this.raycast=null,this.mouse=null,super.destroy()}}class f extends l{orbit;constructor(e){super(e),this.orbit=new r.Orbit(this.camera,{element:this.canvas,target:new r.Vec3(0,0,0),minPolarAngle:Math.PI/2,maxPolarAngle:Math.PI/2,enableRotate:!0,enableZoom:!1,enablePan:!1})}update(){this.orbit?.update()}}class y{type;position;normal;uv;material;constructor(e){this.type=e.type,this.position=e.position,this.normal=e.normal??[],this.uv=e.uv??[],this.material=e.material}}var c=(s=>(s[s.Cube=0]="Cube",s[s.Sphere=1]="Sphere",s[s.Plane=2]="Plane",s[s.Cylinder=3]="Cylinder",s[s.Custom=4]="Custom",s))(c||{});class b{positions=[];normals=[];uvs=[];tmpPositions=[];tmpNormals=[];tmpUVs=[];load(e){const t=e.split(`
33
+ `);for(const h of t){if(!h.trim()||h.startsWith("#"))continue;const a=h.trim().split(/\s+/);switch(a[0]){case"v":this.tmpPositions.push(a.slice(1).map(Number));break;case"vn":this.tmpNormals.push(a.slice(1).map(Number));break;case"vt":this.tmpUVs.push(a.slice(1).map(Number));break;case"f":this.processFaceLine(a);break}}const i={type:c.Custom,position:this.positions,...this.normals.length>0&&{normal:this.normals},...this.uvs.length>0&&{uv:this.uvs}};return new y(i)}processFaceLine(e){for(let t=1;t<e.length;t++){const i=e[t];if(!i)continue;const[h,a,d]=i.split("/"),u=h?parseInt(h,10):void 0,v=a?parseInt(a,10):void 0,p=d?parseInt(d,10):void 0;if(u!==void 0){const o=this.tmpPositions[u-1];o&&this.positions.push(...o)}if(v!==void 0){const o=this.tmpUVs[v-1];o&&this.uvs.push(...o)}if(p!==void 0){const o=this.tmpNormals[p-1];o&&this.normals.push(...o)}}}}n.EditorRenderer=g,n.ObjLoader=b,n.PreviewRenderer=f,n.Renderer=l,n.createProgram=m,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@planara/core",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org/"