@planara/core 1.4.1 → 1.4.3
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
|
|
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,
|
|
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"}
|
package/dist/core/renderer.d.ts
CHANGED
|
@@ -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;
|
|
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;IA4B/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
|
|
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=
|
|
33
|
-
`);for(const
|
|
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;
|
package/dist/index.es.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Texture as v, Program as
|
|
2
|
-
function
|
|
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) {
|
|
3
3
|
const e = (
|
|
4
4
|
/* glsl */
|
|
5
5
|
`
|
|
@@ -20,7 +20,7 @@ function F(s) {
|
|
|
20
20
|
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
21
21
|
}
|
|
22
22
|
`
|
|
23
|
-
),
|
|
23
|
+
), s = (
|
|
24
24
|
/* glsl */
|
|
25
25
|
`
|
|
26
26
|
precision highp float;
|
|
@@ -37,10 +37,10 @@ function F(s) {
|
|
|
37
37
|
gl_FragColor.a = 1.0;
|
|
38
38
|
}
|
|
39
39
|
`
|
|
40
|
-
), i = new v(
|
|
41
|
-
return new
|
|
40
|
+
), i = new v(t);
|
|
41
|
+
return new p(t, {
|
|
42
42
|
vertex: e,
|
|
43
|
-
fragment:
|
|
43
|
+
fragment: s,
|
|
44
44
|
uniforms: { tMap: { value: i } }
|
|
45
45
|
});
|
|
46
46
|
}
|
|
@@ -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
|
|
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), this.meshes = [];
|
|
66
66
|
}
|
|
67
67
|
/**
|
|
68
68
|
* Обновляет размер рендерера и камеры при изменении размеров canvas.
|
|
@@ -92,24 +92,30 @@ class u {
|
|
|
92
92
|
* @param figure Данные фигуры: position, normal, uv
|
|
93
93
|
*/
|
|
94
94
|
addFigure(e) {
|
|
95
|
-
const
|
|
95
|
+
const s = new w(this.gl.gl, {
|
|
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
|
|
100
|
-
geometry:
|
|
99
|
+
}), i = new b(this.gl.gl, {
|
|
100
|
+
geometry: s,
|
|
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
|
|
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
|
|
121
|
-
|
|
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;
|
|
122
128
|
}
|
|
123
129
|
/**
|
|
124
130
|
* Обновление состояния рендерера.
|
|
@@ -132,14 +138,14 @@ class N extends u {
|
|
|
132
138
|
* @param figure Данные фигуры: position, normal, uv
|
|
133
139
|
*/
|
|
134
140
|
addFigure(e) {
|
|
135
|
-
const
|
|
136
|
-
if (
|
|
137
|
-
const i =
|
|
138
|
-
|
|
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";
|
|
139
145
|
}
|
|
140
|
-
return
|
|
146
|
+
return s.isHit = !1, s.onBeforeRender(({ mesh: i }) => {
|
|
141
147
|
this.updateHitUniform(i);
|
|
142
|
-
}),
|
|
148
|
+
}), this.isEventListenersAdded && (this.initMouseListeners(), this.isEventListenersAdded = !0), s;
|
|
143
149
|
}
|
|
144
150
|
/**
|
|
145
151
|
* Обновление uniform uHit для конкретной 3D-модели
|
|
@@ -161,8 +167,12 @@ class N extends u {
|
|
|
161
167
|
handleMouseMove = (e) => {
|
|
162
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);
|
|
163
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
|
+
}
|
|
164
174
|
}
|
|
165
|
-
class
|
|
175
|
+
class I extends u {
|
|
166
176
|
/** Orbit-контроллер для управления камерой */
|
|
167
177
|
orbit;
|
|
168
178
|
/**
|
|
@@ -172,7 +182,7 @@ class U extends u {
|
|
|
172
182
|
constructor(e) {
|
|
173
183
|
super(e), this.orbit = new c(this.camera, {
|
|
174
184
|
element: this.canvas,
|
|
175
|
-
target: new
|
|
185
|
+
target: new L(0, 0, 0),
|
|
176
186
|
minPolarAngle: Math.PI / 2,
|
|
177
187
|
maxPolarAngle: Math.PI / 2,
|
|
178
188
|
enableRotate: !0,
|
|
@@ -187,7 +197,7 @@ class U extends u {
|
|
|
187
197
|
this.orbit?.update();
|
|
188
198
|
}
|
|
189
199
|
}
|
|
190
|
-
class
|
|
200
|
+
class z {
|
|
191
201
|
/** Тип фигуры */
|
|
192
202
|
type;
|
|
193
203
|
/** Позиции вершин */
|
|
@@ -206,8 +216,8 @@ class I {
|
|
|
206
216
|
this.type = e.type, this.position = e.position, this.normal = e.normal ?? [], this.uv = e.uv ?? [], this.material = e.material;
|
|
207
217
|
}
|
|
208
218
|
}
|
|
209
|
-
var
|
|
210
|
-
class
|
|
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 {
|
|
211
221
|
/** Позиции вершин */
|
|
212
222
|
positions = [];
|
|
213
223
|
/** Нормали вершин */
|
|
@@ -223,11 +233,11 @@ class A {
|
|
|
223
233
|
* @param objContent Строка содержимого .obj файла
|
|
224
234
|
*/
|
|
225
235
|
load(e) {
|
|
226
|
-
const
|
|
236
|
+
const s = e.split(`
|
|
227
237
|
`);
|
|
228
|
-
for (const
|
|
229
|
-
if (!
|
|
230
|
-
const r =
|
|
238
|
+
for (const n of s) {
|
|
239
|
+
if (!n.trim() || n.startsWith("#")) continue;
|
|
240
|
+
const r = n.trim().split(/\s+/);
|
|
231
241
|
switch (r[0]) {
|
|
232
242
|
case "v":
|
|
233
243
|
this.tmpPositions.push(r.slice(1).map(Number));
|
|
@@ -244,40 +254,40 @@ class A {
|
|
|
244
254
|
}
|
|
245
255
|
}
|
|
246
256
|
const i = {
|
|
247
|
-
type:
|
|
257
|
+
type: d.Custom,
|
|
248
258
|
position: this.positions,
|
|
249
259
|
...this.normals.length > 0 && { normal: this.normals },
|
|
250
260
|
...this.uvs.length > 0 && { uv: this.uvs }
|
|
251
261
|
};
|
|
252
|
-
return new
|
|
262
|
+
return new z(i);
|
|
253
263
|
}
|
|
254
264
|
/**
|
|
255
265
|
* Обрабатывает строку face (f) и разворачивает индексы в массивы для рендеринга
|
|
256
266
|
*/
|
|
257
267
|
processFaceLine(e) {
|
|
258
|
-
for (let
|
|
259
|
-
const i = e[
|
|
268
|
+
for (let s = 1; s < e.length; s++) {
|
|
269
|
+
const i = e[s];
|
|
260
270
|
if (!i) continue;
|
|
261
|
-
const [
|
|
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;
|
|
262
272
|
if (h !== void 0) {
|
|
263
273
|
const a = this.tmpPositions[h - 1];
|
|
264
274
|
a && this.positions.push(...a);
|
|
265
275
|
}
|
|
266
|
-
if (
|
|
267
|
-
const a = this.tmpUVs[
|
|
276
|
+
if (l !== void 0) {
|
|
277
|
+
const a = this.tmpUVs[l - 1];
|
|
268
278
|
a && this.uvs.push(...a);
|
|
269
279
|
}
|
|
270
|
-
if (
|
|
271
|
-
const a = this.tmpNormals[
|
|
280
|
+
if (m !== void 0) {
|
|
281
|
+
const a = this.tmpNormals[m - 1];
|
|
272
282
|
a && this.normals.push(...a);
|
|
273
283
|
}
|
|
274
284
|
}
|
|
275
285
|
}
|
|
276
286
|
}
|
|
277
287
|
export {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
288
|
+
F as EditorRenderer,
|
|
289
|
+
H as ObjLoader,
|
|
290
|
+
I as PreviewRenderer,
|
|
281
291
|
u as Renderer,
|
|
282
|
-
|
|
292
|
+
E as createProgram
|
|
283
293
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(
|
|
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
|
|
33
|
-
`);for(const h of t){if(!h.trim()||h.startsWith("#"))continue;const
|
|
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"})}));
|