@planara/core 1.4.4 → 1.4.6

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
@@ -1,35 +1,30 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("ogl");function d(s){const e=`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("ogl");function d(t){const e=`
2
2
  attribute vec3 position;
3
3
  attribute vec3 normal;
4
- attribute vec2 uv;
5
4
 
6
5
  uniform mat4 modelViewMatrix;
7
6
  uniform mat4 projectionMatrix;
8
7
  uniform mat3 normalMatrix;
9
8
 
10
9
  varying vec3 vNormal;
11
- varying vec2 vUv;
12
10
 
13
11
  void main() {
14
- vUv = uv;
15
12
  vNormal = normalize(normalMatrix * normal);
16
13
  gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
17
14
  }
18
- `,t=`
15
+ `,s=`
19
16
  precision highp float;
20
17
 
21
18
  uniform float uHit;
22
19
 
23
- uniform sampler2D tMap;
24
20
  varying vec3 vNormal;
25
- varying vec2 vUv;
26
21
 
27
22
  void main() {
28
- vec3 tex = texture2D(tMap, vUv).rgb;
29
- vec3 light = normalize(vec3(0.5, 1.0, -0.3));
30
- float shading = dot(normalize(vNormal), light) * 0.15;
31
- gl_FragColor.rgb = tex + shading;
23
+ vec3 normal = normalize(vNormal);
24
+ float lighting = dot(normal, normalize(vec3(-0.3, 0.8, 0.6)));
25
+ vec3 color = mix(vec3(0.2, 0.8, 1.0), vec3(1.0, 0.2, 0.8), uHit);
26
+ gl_FragColor.rgb = color + lighting * 0.1;
32
27
  gl_FragColor.a = 1.0;
33
28
  }
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;
29
+ `;return new r.Program(t,{vertex:e,fragment:s,cullFace:!1,uniforms:{uHit:{value:0}}})}class h{gl;scene;camera;canvas;program;meshes;constructor(e){this.canvas=e,this.gl=new r.Renderer({canvas:e,dpr:2}),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 s=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:s,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 v extends h{orbit;raycast;mouse;isEventListenersAdded;constructor(e){super(e);const s=new r.GridHelper(this.gl.gl,{size:10,divisions:10});s.position.y=-.001,s.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 s=super.addFigure(e);if(s.geometry){const i=s.geometry.constructor.name;s.geometry.raycast=i.includes("Sphere")?"sphere":"box"}return s.isHit=!1,s.onBeforeRender(({mesh:i})=>{this.updateHitUniform(i)}),this.isEventListenersAdded&&(this.initMouseListeners(),this.isEventListenersAdded=!0),s}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 p=(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))(p||{});class y{positions=[];normals=[];uvs=[];tmpPositions=[];tmpNormals=[];tmpUVs=[];load(e){const s=e.split(`
30
+ `);for(const a of s){if(!a.trim()||a.startsWith("#"))continue;const n=a.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:p.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 s=1;s<e.length;s++){const i=e[s];if(!i)continue;const[a,n,l]=i.split("/"),m=a?parseInt(a,10):void 0,c=n?parseInt(n,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=y;exports.PreviewRenderer=g;exports.Renderer=h;exports.createProgram=d;
package/dist/index.es.js CHANGED
@@ -1,50 +1,45 @@
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) {
1
+ import { Program as p, Renderer as v, Transform as g, Camera as f, Geometry as y, Mesh as w, GridHelper as b, AxesHelper as x, Orbit as c, Raycast as M, Vec2 as P, Vec3 as A } from "ogl";
2
+ function L(t) {
3
3
  const e = (
4
4
  /* glsl */
5
5
  `
6
6
  attribute vec3 position;
7
7
  attribute vec3 normal;
8
- attribute vec2 uv;
9
8
 
10
9
  uniform mat4 modelViewMatrix;
11
10
  uniform mat4 projectionMatrix;
12
11
  uniform mat3 normalMatrix;
13
12
 
14
13
  varying vec3 vNormal;
15
- varying vec2 vUv;
16
14
 
17
15
  void main() {
18
- vUv = uv;
19
16
  vNormal = normalize(normalMatrix * normal);
20
17
  gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
21
18
  }
22
19
  `
23
- ), t = (
20
+ ), s = (
24
21
  /* glsl */
25
22
  `
26
23
  precision highp float;
27
24
 
28
25
  uniform float uHit;
29
26
 
30
- uniform sampler2D tMap;
31
27
  varying vec3 vNormal;
32
- varying vec2 vUv;
33
28
 
34
29
  void main() {
35
- vec3 tex = texture2D(tMap, vUv).rgb;
36
- vec3 light = normalize(vec3(0.5, 1.0, -0.3));
37
- float shading = dot(normalize(vNormal), light) * 0.15;
38
- gl_FragColor.rgb = tex + shading;
30
+ vec3 normal = normalize(vNormal);
31
+ float lighting = dot(normal, normalize(vec3(-0.3, 0.8, 0.6)));
32
+ vec3 color = mix(vec3(0.2, 0.8, 1.0), vec3(1.0, 0.2, 0.8), uHit);
33
+ gl_FragColor.rgb = color + lighting * 0.1;
39
34
  gl_FragColor.a = 1.0;
40
35
  }
41
36
  `
42
- ), i = new v(s);
43
- return new p(s, {
37
+ );
38
+ return new p(t, {
44
39
  vertex: e,
45
- fragment: t,
40
+ fragment: s,
41
+ cullFace: !1,
46
42
  uniforms: {
47
- tMap: { value: i },
48
43
  uHit: { value: 0 }
49
44
  }
50
45
  });
@@ -67,7 +62,7 @@ class u {
67
62
  * @param canvas - HTMLCanvasElement для рендеринга
68
63
  */
69
64
  constructor(e) {
70
- 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), this.meshes = [];
65
+ this.canvas = e, this.gl = new v({ canvas: e, dpr: 2 }), this.gl.setSize(e.clientWidth, e.clientHeight), this.gl.gl.clearColor(1, 1, 1, 1), this.scene = new g(), this.camera = new f(this.gl.gl, { fov: 45 }), this.camera.position.set(1, 1, 7), this.camera.lookAt([0, 0, 0]), this.program = L(this.gl.gl), this.meshes = [];
71
66
  }
72
67
  /**
73
68
  * Обновляет размер рендерера и камеры при изменении размеров canvas.
@@ -97,12 +92,12 @@ class u {
97
92
  * @param figure Данные фигуры: position, normal, uv
98
93
  */
99
94
  addFigure(e) {
100
- const t = new w(this.gl.gl, {
95
+ const s = new y(this.gl.gl, {
101
96
  position: { size: 3, data: new Float32Array(e.position) },
102
97
  normal: { size: 3, data: new Float32Array(e.normal ?? []) },
103
98
  uv: { size: 2, data: new Float32Array(e.uv ?? []) }
104
- }), i = new b(this.gl.gl, {
105
- geometry: t,
99
+ }), i = new w(this.gl.gl, {
100
+ geometry: s,
106
101
  program: this.program
107
102
  });
108
103
  return i.setParent(this.scene), this.meshes.push(i), i;
@@ -112,7 +107,7 @@ class u {
112
107
  this.meshes && (this.meshes.length = 0, this.meshes = []), this.scene = null, this.camera = null, this.program = null, this.gl = null, this.canvas = null;
113
108
  }
114
109
  }
115
- class C extends u {
110
+ class F extends u {
116
111
  /** Orbit-контроллер для управления камерой */
117
112
  orbit;
118
113
  /** Raycast для подсветки моделей при наведении */
@@ -128,8 +123,8 @@ class C extends u {
128
123
  */
129
124
  constructor(e) {
130
125
  super(e);
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;
126
+ const s = new b(this.gl.gl, { size: 10, divisions: 10 });
127
+ s.position.y = -1e-3, s.setParent(this.scene), new x(this.gl.gl, { size: 6, symmetric: !0 }).setParent(this.scene), this.orbit = new c(this.camera, { element: this.canvas }), this.raycast = new M(), this.mouse = new P(), this.isEventListenersAdded = !1;
133
128
  }
134
129
  /**
135
130
  * Обновление состояния рендерера.
@@ -143,14 +138,14 @@ class C extends u {
143
138
  * @param figure Данные фигуры: position, normal, uv
144
139
  */
145
140
  addFigure(e) {
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";
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";
150
145
  }
151
- return t.isHit = !1, t.onBeforeRender(({ mesh: i }) => {
146
+ return s.isHit = !1, s.onBeforeRender(({ mesh: i }) => {
152
147
  this.updateHitUniform(i);
153
- }), this.isEventListenersAdded && (this.initMouseListeners(), this.isEventListenersAdded = !0), t;
148
+ }), this.isEventListenersAdded && (this.initMouseListeners(), this.isEventListenersAdded = !0), s;
154
149
  }
155
150
  /**
156
151
  * Обновление uniform uHit для конкретной 3D-модели
@@ -177,7 +172,7 @@ class C extends u {
177
172
  this.isEventListenersAdded && (window.removeEventListener("mousemove", this.handleMouseMove, !1), this.isEventListenersAdded = !1), this.orbit = null, this.raycast = null, this.mouse = null, super.destroy();
178
173
  }
179
174
  }
180
- class F extends u {
175
+ class z extends u {
181
176
  /** Orbit-контроллер для управления камерой */
182
177
  orbit;
183
178
  /**
@@ -187,7 +182,7 @@ class F extends u {
187
182
  constructor(e) {
188
183
  super(e), this.orbit = new c(this.camera, {
189
184
  element: this.canvas,
190
- target: new L(0, 0, 0),
185
+ target: new A(0, 0, 0),
191
186
  minPolarAngle: Math.PI / 2,
192
187
  maxPolarAngle: Math.PI / 2,
193
188
  enableRotate: !0,
@@ -221,8 +216,8 @@ class H {
221
216
  this.type = e.type, this.position = e.position, this.normal = e.normal ?? [], this.uv = e.uv ?? [], this.material = e.material;
222
217
  }
223
218
  }
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 {
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 C {
226
221
  /** Позиции вершин */
227
222
  positions = [];
228
223
  /** Нормали вершин */
@@ -238,11 +233,11 @@ class I {
238
233
  * @param objContent Строка содержимого .obj файла
239
234
  */
240
235
  load(e) {
241
- const t = e.split(`
236
+ const s = e.split(`
242
237
  `);
243
- for (const n of t) {
244
- if (!n.trim() || n.startsWith("#")) continue;
245
- const r = n.trim().split(/\s+/);
238
+ for (const a of s) {
239
+ if (!a.trim() || a.startsWith("#")) continue;
240
+ const r = a.trim().split(/\s+/);
246
241
  switch (r[0]) {
247
242
  case "v":
248
243
  this.tmpPositions.push(r.slice(1).map(Number));
@@ -270,29 +265,29 @@ class I {
270
265
  * Обрабатывает строку face (f) и разворачивает индексы в массивы для рендеринга
271
266
  */
272
267
  processFaceLine(e) {
273
- for (let t = 1; t < e.length; t++) {
274
- const i = e[t];
268
+ for (let s = 1; s < e.length; s++) {
269
+ const i = e[s];
275
270
  if (!i) continue;
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;
271
+ const [a, r, o] = i.split("/"), h = a ? parseInt(a, 10) : void 0, l = r ? parseInt(r, 10) : void 0, m = o ? parseInt(o, 10) : void 0;
277
272
  if (h !== void 0) {
278
- const a = this.tmpPositions[h - 1];
279
- a && this.positions.push(...a);
273
+ const n = this.tmpPositions[h - 1];
274
+ n && this.positions.push(...n);
280
275
  }
281
276
  if (l !== void 0) {
282
- const a = this.tmpUVs[l - 1];
283
- a && this.uvs.push(...a);
277
+ const n = this.tmpUVs[l - 1];
278
+ n && this.uvs.push(...n);
284
279
  }
285
280
  if (m !== void 0) {
286
- const a = this.tmpNormals[m - 1];
287
- a && this.normals.push(...a);
281
+ const n = this.tmpNormals[m - 1];
282
+ n && this.normals.push(...n);
288
283
  }
289
284
  }
290
285
  }
291
286
  }
292
287
  export {
293
- C as EditorRenderer,
294
- I as ObjLoader,
295
- F as PreviewRenderer,
288
+ F as EditorRenderer,
289
+ C as ObjLoader,
290
+ z as PreviewRenderer,
296
291
  u as Renderer,
297
- E as createProgram
292
+ L as createProgram
298
293
  };
package/dist/index.umd.js CHANGED
@@ -1,17 +1,14 @@
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=`
1
+ (function(n,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("ogl")):typeof define=="function"&&define.amd?define(["exports","ogl"],i):(n=typeof globalThis<"u"?globalThis:n||self,i(n.PlanaraCore={},n.OGL))})(this,(function(n,i){"use strict";function m(s){const e=`
2
2
  attribute vec3 position;
3
3
  attribute vec3 normal;
4
- attribute vec2 uv;
5
4
 
6
5
  uniform mat4 modelViewMatrix;
7
6
  uniform mat4 projectionMatrix;
8
7
  uniform mat3 normalMatrix;
9
8
 
10
9
  varying vec3 vNormal;
11
- varying vec2 vUv;
12
10
 
13
11
  void main() {
14
- vUv = uv;
15
12
  vNormal = normalize(normalMatrix * normal);
16
13
  gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
17
14
  }
@@ -20,16 +17,14 @@
20
17
 
21
18
  uniform float uHit;
22
19
 
23
- uniform sampler2D tMap;
24
20
  varying vec3 vNormal;
25
- varying vec2 vUv;
26
21
 
27
22
  void main() {
28
- vec3 tex = texture2D(tMap, vUv).rgb;
29
- vec3 light = normalize(vec3(0.5, 1.0, -0.3));
30
- float shading = dot(normalize(vNormal), light) * 0.15;
31
- gl_FragColor.rgb = tex + shading;
23
+ vec3 normal = normalize(vNormal);
24
+ float lighting = dot(normal, normalize(vec3(-0.3, 0.8, 0.6)));
25
+ vec3 color = mix(vec3(0.2, 0.8, 1.0), vec3(1.0, 0.2, 0.8), uHit);
26
+ gl_FragColor.rgb = color + lighting * 0.1;
32
27
  gl_FragColor.a = 1.0;
33
28
  }
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"})}));
29
+ `;return new i.Program(s,{vertex:e,fragment:t,cullFace:!1,uniforms:{uHit:{value:0}}})}class l{gl;scene;camera;canvas;program;meshes;constructor(e){this.canvas=e,this.gl=new i.Renderer({canvas:e,dpr:2}),this.gl.setSize(e.clientWidth,e.clientHeight),this.gl.gl.clearColor(1,1,1,1),this.scene=new i.Transform,this.camera=new i.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 i.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??[])}}),r=new i.Mesh(this.gl.gl,{geometry:t,program:this.program});return r.setParent(this.scene),this.meshes.push(r),r}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 i.GridHelper(this.gl.gl,{size:10,divisions:10});t.position.y=-.001,t.setParent(this.scene),new i.AxesHelper(this.gl.gl,{size:6,symmetric:!0}).setParent(this.scene),this.orbit=new i.Orbit(this.camera,{element:this.canvas}),this.raycast=new i.Raycast,this.mouse=new i.Vec2,this.isEventListenersAdded=!1}update(){this.orbit?.update()}addFigure(e){const t=super.addFigure(e);if(t.geometry){const r=t.geometry.constructor.name;t.geometry.raycast=r.includes("Sphere")?"sphere":"box"}return t.isHit=!1,t.onBeforeRender(({mesh:r})=>{this.updateHitUniform(r)}),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(r=>r.isHit=!1),this.raycast.intersectBounds(this.meshes).forEach(r=>r.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 i.Orbit(this.camera,{element:this.canvas,target:new i.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(`
30
+ `);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 r={type:c.Custom,position:this.positions,...this.normals.length>0&&{normal:this.normals},...this.uvs.length>0&&{uv:this.uvs}};return new y(r)}processFaceLine(e){for(let t=1;t<e.length;t++){const r=e[t];if(!r)continue;const[h,a,d]=r.split("/"),u=h?parseInt(h,10):void 0,p=a?parseInt(a,10):void 0,v=d?parseInt(d,10):void 0;if(u!==void 0){const o=this.tmpPositions[u-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)}}}}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,CAgD9D"}
1
+ {"version":3,"file":"program-settings.d.ts","sourceRoot":"","sources":["../../src/utils/program-settings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,mBAAmB,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAExD;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,EAAE,EAAE,mBAAmB,GAAG,OAAO,CAyC9D"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@planara/core",
3
- "version": "1.4.4",
3
+ "version": "1.4.6",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org/"