@planara/core 1.4.3 → 1.4.4

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.
package/dist/index.cjs.js CHANGED
@@ -18,6 +18,8 @@
18
18
  `,t=`
19
19
  precision highp float;
20
20
 
21
+ uniform float uHit;
22
+
21
23
  uniform sampler2D tMap;
22
24
  varying vec3 vNormal;
23
25
  varying vec2 vUv;
@@ -29,5 +31,5 @@
29
31
  gl_FragColor.rgb = tex + shading;
30
32
  gl_FragColor.a = 1.0;
31
33
  }
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),this.meshes=[]}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;
34
+ `,i=new r.Texture(s);return new r.Program(s,{vertex:e,fragment:t,uniforms:{tMap:{value:i},uHit:{value:0}}})}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),this.meshes=[]}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(`
35
+ `);for(const o of t){if(!o.trim()||o.startsWith("#"))continue;const a=o.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: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,a,l]=i.split("/"),m=o?parseInt(o,10):void 0,c=a?parseInt(a,10):void 0,u=l?parseInt(l,10):void 0;if(m!==void 0){const n=this.tmpPositions[m-1];n&&this.positions.push(...n)}if(c!==void 0){const n=this.tmpUVs[c-1];n&&this.uvs.push(...n)}if(u!==void 0){const n=this.tmpNormals[u-1];n&&this.normals.push(...n)}}}}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
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(t) {
2
+ function E(s) {
3
3
  const e = (
4
4
  /* glsl */
5
5
  `
@@ -20,11 +20,13 @@ function E(t) {
20
20
  gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
21
21
  }
22
22
  `
23
- ), s = (
23
+ ), t = (
24
24
  /* glsl */
25
25
  `
26
26
  precision highp float;
27
27
 
28
+ uniform float uHit;
29
+
28
30
  uniform sampler2D tMap;
29
31
  varying vec3 vNormal;
30
32
  varying vec2 vUv;
@@ -37,11 +39,14 @@ function E(t) {
37
39
  gl_FragColor.a = 1.0;
38
40
  }
39
41
  `
40
- ), i = new v(t);
41
- return new p(t, {
42
+ ), i = new v(s);
43
+ return new p(s, {
42
44
  vertex: e,
43
- fragment: s,
44
- uniforms: { tMap: { value: i } }
45
+ fragment: t,
46
+ uniforms: {
47
+ tMap: { value: i },
48
+ uHit: { value: 0 }
49
+ }
45
50
  });
46
51
  }
47
52
  class u {
@@ -92,12 +97,12 @@ class u {
92
97
  * @param figure Данные фигуры: position, normal, uv
93
98
  */
94
99
  addFigure(e) {
95
- const s = new w(this.gl.gl, {
100
+ const t = new w(this.gl.gl, {
96
101
  position: { size: 3, data: new Float32Array(e.position) },
97
102
  normal: { size: 3, data: new Float32Array(e.normal ?? []) },
98
103
  uv: { size: 2, data: new Float32Array(e.uv ?? []) }
99
104
  }), i = new b(this.gl.gl, {
100
- geometry: s,
105
+ geometry: t,
101
106
  program: this.program
102
107
  });
103
108
  return i.setParent(this.scene), this.meshes.push(i), i;
@@ -107,7 +112,7 @@ class u {
107
112
  this.meshes && (this.meshes.length = 0, this.meshes = []), this.scene = null, this.camera = null, this.program = null, this.gl = null, this.canvas = null;
108
113
  }
109
114
  }
110
- class F extends u {
115
+ class C extends u {
111
116
  /** Orbit-контроллер для управления камерой */
112
117
  orbit;
113
118
  /** Raycast для подсветки моделей при наведении */
@@ -123,8 +128,8 @@ class F extends u {
123
128
  */
124
129
  constructor(e) {
125
130
  super(e);
126
- const s = new x(this.gl.gl, { size: 10, divisions: 10 });
127
- s.position.y = -1e-3, s.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;
131
+ const t = new x(this.gl.gl, { size: 10, divisions: 10 });
132
+ 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;
128
133
  }
129
134
  /**
130
135
  * Обновление состояния рендерера.
@@ -138,14 +143,14 @@ class F extends u {
138
143
  * @param figure Данные фигуры: position, normal, uv
139
144
  */
140
145
  addFigure(e) {
141
- const s = super.addFigure(e);
142
- if (s.geometry) {
143
- const i = s.geometry.constructor.name;
144
- s.geometry.raycast = i.includes("Sphere") ? "sphere" : "box";
146
+ const t = super.addFigure(e);
147
+ if (t.geometry) {
148
+ const i = t.geometry.constructor.name;
149
+ t.geometry.raycast = i.includes("Sphere") ? "sphere" : "box";
145
150
  }
146
- return s.isHit = !1, s.onBeforeRender(({ mesh: i }) => {
151
+ return t.isHit = !1, t.onBeforeRender(({ mesh: i }) => {
147
152
  this.updateHitUniform(i);
148
- }), this.isEventListenersAdded && (this.initMouseListeners(), this.isEventListenersAdded = !0), s;
153
+ }), this.isEventListenersAdded && (this.initMouseListeners(), this.isEventListenersAdded = !0), t;
149
154
  }
150
155
  /**
151
156
  * Обновление uniform uHit для конкретной 3D-модели
@@ -172,7 +177,7 @@ class F extends u {
172
177
  this.isEventListenersAdded && (window.removeEventListener("mousemove", this.handleMouseMove, !1), this.isEventListenersAdded = !1), this.orbit = null, this.raycast = null, this.mouse = null, super.destroy();
173
178
  }
174
179
  }
175
- class I extends u {
180
+ class F extends u {
176
181
  /** Orbit-контроллер для управления камерой */
177
182
  orbit;
178
183
  /**
@@ -197,7 +202,7 @@ class I extends u {
197
202
  this.orbit?.update();
198
203
  }
199
204
  }
200
- class z {
205
+ class H {
201
206
  /** Тип фигуры */
202
207
  type;
203
208
  /** Позиции вершин */
@@ -216,8 +221,8 @@ class z {
216
221
  this.type = e.type, this.position = e.position, this.normal = e.normal ?? [], this.uv = e.uv ?? [], this.material = e.material;
217
222
  }
218
223
  }
219
- var d = /* @__PURE__ */ ((t) => (t[t.Cube = 0] = "Cube", t[t.Sphere = 1] = "Sphere", t[t.Plane = 2] = "Plane", t[t.Cylinder = 3] = "Cylinder", t[t.Custom = 4] = "Custom", t))(d || {});
220
- class H {
224
+ 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 || {});
225
+ class I {
221
226
  /** Позиции вершин */
222
227
  positions = [];
223
228
  /** Нормали вершин */
@@ -233,9 +238,9 @@ class H {
233
238
  * @param objContent Строка содержимого .obj файла
234
239
  */
235
240
  load(e) {
236
- const s = e.split(`
241
+ const t = e.split(`
237
242
  `);
238
- for (const n of s) {
243
+ for (const n of t) {
239
244
  if (!n.trim() || n.startsWith("#")) continue;
240
245
  const r = n.trim().split(/\s+/);
241
246
  switch (r[0]) {
@@ -259,14 +264,14 @@ class H {
259
264
  ...this.normals.length > 0 && { normal: this.normals },
260
265
  ...this.uvs.length > 0 && { uv: this.uvs }
261
266
  };
262
- return new z(i);
267
+ return new H(i);
263
268
  }
264
269
  /**
265
270
  * Обрабатывает строку face (f) и разворачивает индексы в массивы для рендеринга
266
271
  */
267
272
  processFaceLine(e) {
268
- for (let s = 1; s < e.length; s++) {
269
- const i = e[s];
273
+ for (let t = 1; t < e.length; t++) {
274
+ const i = e[t];
270
275
  if (!i) continue;
271
276
  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;
272
277
  if (h !== void 0) {
@@ -285,9 +290,9 @@ class H {
285
290
  }
286
291
  }
287
292
  export {
288
- F as EditorRenderer,
289
- H as ObjLoader,
290
- I as PreviewRenderer,
293
+ C as EditorRenderer,
294
+ I as ObjLoader,
295
+ F as PreviewRenderer,
291
296
  u as Renderer,
292
297
  E as createProgram
293
298
  };
package/dist/index.umd.js CHANGED
@@ -18,6 +18,8 @@
18
18
  `,t=`
19
19
  precision highp float;
20
20
 
21
+ uniform float uHit;
22
+
21
23
  uniform sampler2D tMap;
22
24
  varying vec3 vNormal;
23
25
  varying vec2 vUv;
@@ -29,5 +31,5 @@
29
31
  gl_FragColor.rgb = tex + shading;
30
32
  gl_FragColor.a = 1.0;
31
33
  }
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),this.meshes=[]}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"})}));
34
+ `,i=new r.Texture(s);return new r.Program(s,{vertex:e,fragment:t,uniforms:{tMap:{value:i},uHit:{value:0}}})}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),this.meshes=[]}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 f 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 g 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(`
35
+ `);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,u]=i.split("/"),d=h?parseInt(h,10):void 0,v=a?parseInt(a,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)}}}}n.EditorRenderer=f,n.ObjLoader=b,n.PreviewRenderer=g,n.Renderer=l,n.createProgram=m,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
@@ -1 +1 @@
1
- {"version":3,"file":"program-settings.d.ts","sourceRoot":"","sources":["../../src/utils/program-settings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,mBAAmB,EAAE,OAAO,EAAW,MAAM,KAAK,CAAC;AAEjE;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,EAAE,EAAE,mBAAmB,GAAG,OAAO,CA2C9D"}
1
+ {"version":3,"file":"program-settings.d.ts","sourceRoot":"","sources":["../../src/utils/program-settings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,mBAAmB,EAAE,OAAO,EAAW,MAAM,KAAK,CAAC;AAEjE;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,EAAE,EAAE,mBAAmB,GAAG,OAAO,CAgD9D"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@planara/core",
3
- "version": "1.4.3",
3
+ "version": "1.4.4",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org/"