@planara/core 1.4.0 → 1.4.1

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.
@@ -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,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;IAM1B;;OAEG;IACH,OAAO,CAAC,eAAe,CAerB;CACH"}
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 p(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=p(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 v 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)})}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)}}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,l]=i.split("/"),m=n?parseInt(n,10):void 0,c=a?parseInt(a,10):void 0,u=l?parseInt(l,10):void 0;if(m!==void 0){const o=this.tmpPositions[m-1];o&&this.positions.push(...o)}if(c!==void 0){const o=this.tmpUVs[c-1];o&&this.uvs.push(...o)}if(u!==void 0){const o=this.tmpNormals[u-1];o&&this.normals.push(...o)}}}}exports.EditorRenderer=v;exports.ObjLoader=b;exports.PreviewRenderer=g;exports.Renderer=h;exports.createProgram=p;
package/dist/index.es.js CHANGED
@@ -1,4 +1,4 @@
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";
1
+ import { Texture as v, 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 c, Raycast as P, Vec2 as z, Vec3 as C } from "ogl";
2
2
  function F(s) {
3
3
  const e = (
4
4
  /* glsl */
@@ -37,7 +37,7 @@ function F(s) {
37
37
  gl_FragColor.a = 1.0;
38
38
  }
39
39
  `
40
- ), i = new p(s);
40
+ ), i = new v(s);
41
41
  return new d(s, {
42
42
  vertex: e,
43
43
  fragment: t,
@@ -118,7 +118,7 @@ class N extends u {
118
118
  constructor(e) {
119
119
  super(e);
120
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();
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 c(this.camera, { element: this.canvas }), this.raycast = new P(), this.mouse = new z(), this.initMouseListeners();
122
122
  }
123
123
  /**
124
124
  * Обновление состояния рендерера.
@@ -152,21 +152,14 @@ class N extends u {
152
152
  */
153
153
  initMouseListeners() {
154
154
  window.addEventListener("load", () => {
155
- document.addEventListener("mousemove", this.handleMouseMove, !1), document.addEventListener("touchmove", this.handleMouseMove, !1);
155
+ document.addEventListener("mousemove", this.handleMouseMove, !1);
156
156
  });
157
157
  }
158
158
  /**
159
159
  * Обработчик движения мыши
160
160
  */
161
161
  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);
162
+ 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
163
  };
171
164
  }
172
165
  class U extends u {
@@ -177,7 +170,7 @@ class U extends u {
177
170
  * @param canvas - HTMLCanvasElement для рендеринга
178
171
  */
179
172
  constructor(e) {
180
- super(e), this.orbit = new m(this.camera, {
173
+ super(e), this.orbit = new c(this.camera, {
181
174
  element: this.canvas,
182
175
  target: new C(0, 0, 0),
183
176
  minPolarAngle: Math.PI / 2,
@@ -213,7 +206,7 @@ class I {
213
206
  this.type = e.type, this.position = e.position, this.normal = e.normal ?? [], this.uv = e.uv ?? [], this.material = e.material;
214
207
  }
215
208
  }
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 || {});
209
+ var p = /* @__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))(p || {});
217
210
  class A {
218
211
  /** Позиции вершин */
219
212
  positions = [];
@@ -251,7 +244,7 @@ class A {
251
244
  }
252
245
  }
253
246
  const i = {
254
- type: v.Custom,
247
+ type: p.Custom,
255
248
  position: this.positions,
256
249
  ...this.normals.length > 0 && { normal: this.normals },
257
250
  ...this.uvs.length > 0 && { uv: this.uvs }
@@ -265,13 +258,13 @@ class A {
265
258
  for (let t = 1; t < e.length; t++) {
266
259
  const i = e[t];
267
260
  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;
261
+ const [o, r, n] = i.split("/"), h = o ? parseInt(o, 10) : void 0, m = r ? parseInt(r, 10) : void 0, l = n ? parseInt(n, 10) : void 0;
269
262
  if (h !== void 0) {
270
263
  const a = this.tmpPositions[h - 1];
271
264
  a && this.positions.push(...a);
272
265
  }
273
- if (c !== void 0) {
274
- const a = this.tmpUVs[c - 1];
266
+ if (m !== void 0) {
267
+ const a = this.tmpUVs[m - 1];
275
268
  a && this.uvs.push(...a);
276
269
  }
277
270
  if (l !== void 0) {
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(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 c(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 m{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=c(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 g extends m{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)})}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)}}class f extends m{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,p=n?parseInt(n,10):void 0,v=u?parseInt(u,10):void 0;if(d!==void 0){const o=this.tmpPositions[d-1];o&&this.positions.push(...o)}if(p!==void 0){const o=this.tmpUVs[p-1];o&&this.uvs.push(...o)}if(v!==void 0){const o=this.tmpNormals[v-1];o&&this.normals.push(...o)}}}}a.EditorRenderer=g,a.ObjLoader=y,a.PreviewRenderer=f,a.Renderer=m,a.createProgram=c,Object.defineProperty(a,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.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org/"