chorama 0.0.0

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.
Files changed (256) hide show
  1. package/.configs/tsconfig.lib.json +43 -0
  2. package/.configs/tsconfig.website.json +34 -0
  3. package/.github/workflows/static.yml +88 -0
  4. package/.vscode/launch.json +29 -0
  5. package/.vscode/tasks.json +19 -0
  6. package/README.md +127 -0
  7. package/assets/images/disappointed.jpg +0 -0
  8. package/assets/images/skybox/grimmnight_back.png +0 -0
  9. package/assets/images/skybox/grimmnight_bottom.png +0 -0
  10. package/assets/images/skybox/grimmnight_front.png +0 -0
  11. package/assets/images/skybox/grimmnight_left.png +0 -0
  12. package/assets/images/skybox/grimmnight_right.png +0 -0
  13. package/assets/images/skybox/grimmnight_top.png +0 -0
  14. package/assets/images/skybox/miramar_back.png +0 -0
  15. package/assets/images/skybox/miramar_bottom.png +0 -0
  16. package/assets/images/skybox/miramar_front.png +0 -0
  17. package/assets/images/skybox/miramar_left.png +0 -0
  18. package/assets/images/skybox/miramar_right.png +0 -0
  19. package/assets/images/skybox/miramar_top.png +0 -0
  20. package/assets/images/uv.jpg +0 -0
  21. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_BaseColor.png +0 -0
  22. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_Normal.png +0 -0
  23. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_OcclusionRoughMetal.png +0 -0
  24. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_BaseColor.png +0 -0
  25. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_Normal.png +0 -0
  26. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_OcclusionRoughMetal.png +0 -0
  27. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_BaseColor.png +0 -0
  28. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_Normal.png +0 -0
  29. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_OcclusionRoughMetal.png +0 -0
  30. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_BaseColor.png +0 -0
  31. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_Normal.png +0 -0
  32. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_OcclusionRoughMetal.png +0 -0
  33. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_BaseColor.png +0 -0
  34. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_Normal.png +0 -0
  35. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_OcclusionRoughMetal.png +0 -0
  36. package/assets/models/gltf/flight_helmet/index.bin +0 -0
  37. package/assets/models/gltf/flight_helmet/index.gltf +705 -0
  38. package/assets/models/gltf/object.gltf +23 -0
  39. package/assets/models/gltf/pirate_girl/index.bin +0 -0
  40. package/assets/models/gltf/pirate_girl/index.gltf +2082 -0
  41. package/assets/models/obj/pirate_girl/pirate_girl.obj +18459 -0
  42. package/assets/models/obj/pirate_girl/pirate_girl.png +0 -0
  43. package/astro.config.mjs +45 -0
  44. package/content/guide/api-map.md +89 -0
  45. package/content/guide/camera-and-controls.md +98 -0
  46. package/content/guide/first-scene.md +176 -0
  47. package/content/guide/index.md +72 -0
  48. package/content/guide/installation.md +179 -0
  49. package/content/guide/materials-and-lighting.md +138 -0
  50. package/content/guide/plugins-and-render-pipeline.md +124 -0
  51. package/content/guide/render-targets-and-views.md +147 -0
  52. package/content/guide/scene-graph-and-transforms.md +113 -0
  53. package/content/guide/textures-and-assets.md +120 -0
  54. package/content/guide/troubleshooting.md +49 -0
  55. package/env.d.ts +19 -0
  56. package/examples/addons/rendergraph_gui.js +580 -0
  57. package/examples/camera/orthographic.js +120 -0
  58. package/examples/camera/perspective.js +138 -0
  59. package/examples/lights/directional.js +397 -0
  60. package/examples/lights/multiple_spot_lights.js +304 -0
  61. package/examples/lights/point.js +337 -0
  62. package/examples/lights/spot.js +366 -0
  63. package/examples/loader/gltf_material.js +111 -0
  64. package/examples/loader/gltfloader.js +78 -0
  65. package/examples/loader/objloader.js +95 -0
  66. package/examples/material/cullface.js +111 -0
  67. package/examples/material/materials.js +126 -0
  68. package/examples/material/standard/basic.js +164 -0
  69. package/examples/mesh/circle.js +117 -0
  70. package/examples/mesh/cuboid.js +151 -0
  71. package/examples/mesh/cylinder.js +139 -0
  72. package/examples/mesh/geometries.js +108 -0
  73. package/examples/mesh/meshTopology.js +103 -0
  74. package/examples/mesh/plane.js +117 -0
  75. package/examples/mesh/skinning.js +136 -0
  76. package/examples/mesh/uvsphere.js +113 -0
  77. package/examples/other/rotatingCube.js +93 -0
  78. package/examples/other/rotatingSphere.js +96 -0
  79. package/examples/rendertarget/basic_canvas.js +130 -0
  80. package/examples/rendertarget/depth_texture.js +130 -0
  81. package/examples/rendertarget/image_target.js +140 -0
  82. package/examples/rendertarget/multiple_views.js +158 -0
  83. package/examples/rendertarget/render_masks.js +173 -0
  84. package/examples/rendertarget/split_screen.js +123 -0
  85. package/examples/rendertarget/split_view.js +137 -0
  86. package/examples/skybox/skybox.js +111 -0
  87. package/examples/texture/arrays.js +156 -0
  88. package/examples/texture/textureWrap.js +118 -0
  89. package/examples/transform/propagation.js +92 -0
  90. package/package.json +55 -0
  91. package/rollup.config.js +66 -0
  92. package/scripts/stage-chorama.mjs +29 -0
  93. package/src/caches/cache.js +420 -0
  94. package/src/caches/index.js +2 -0
  95. package/src/caches/uniformbuffers.js +104 -0
  96. package/src/cameracontrols/index.js +258 -0
  97. package/src/constants/index.js +3 -0
  98. package/src/constants/mesh.js +197 -0
  99. package/src/constants/others.js +218 -0
  100. package/src/constants/texture.js +183 -0
  101. package/src/core/constants.js +14 -0
  102. package/src/core/extensions.js +42 -0
  103. package/src/core/index.js +7 -0
  104. package/src/core/layouts/index.js +4 -0
  105. package/src/core/layouts/meshvertex.js +60 -0
  106. package/src/core/layouts/uniform.js +21 -0
  107. package/src/core/layouts/uniformbuffer.js +15 -0
  108. package/src/core/layouts/vertexbuffer.js +43 -0
  109. package/src/core/limits.js +247 -0
  110. package/src/core/resources/blendparams.js +89 -0
  111. package/src/core/resources/framebuffer.js +127 -0
  112. package/src/core/resources/gpubuffer.js +32 -0
  113. package/src/core/resources/gpumesh.js +43 -0
  114. package/src/core/resources/gputexture.js +73 -0
  115. package/src/core/resources/index.js +5 -0
  116. package/src/core/shader.js +62 -0
  117. package/src/core/webgl/bindgroup.js +89 -0
  118. package/src/core/webgl/descriptors.js +104 -0
  119. package/src/core/webgl/index.js +5 -0
  120. package/src/core/webgl/renderpassencoder.js +96 -0
  121. package/src/core/webgl/renderpipeline.js +54 -0
  122. package/src/core/webgl/utils.js +371 -0
  123. package/src/core/webgl/webglrenderdevice.js +235 -0
  124. package/src/function.js +358 -0
  125. package/src/index.js +15 -0
  126. package/src/loader/gltf.js +2172 -0
  127. package/src/loader/index.js +3 -0
  128. package/src/loader/loader.js +174 -0
  129. package/src/loader/obj.js +188 -0
  130. package/src/loader/texture.js +85 -0
  131. package/src/loader/utils.js +16 -0
  132. package/src/material/basic.js +75 -0
  133. package/src/material/depth.js +73 -0
  134. package/src/material/index.js +8 -0
  135. package/src/material/lambert.js +73 -0
  136. package/src/material/material.js +106 -0
  137. package/src/material/normal.js +30 -0
  138. package/src/material/phong.js +86 -0
  139. package/src/material/raw.js +52 -0
  140. package/src/material/standard.js +221 -0
  141. package/src/math/index.js +3 -0
  142. package/src/math/transform.js +38 -0
  143. package/src/mesh/attribute/attribute.js +79 -0
  144. package/src/mesh/attribute/index.js +1 -0
  145. package/src/mesh/attributedata/index.js +1 -0
  146. package/src/mesh/attributedata/separate.js +180 -0
  147. package/src/mesh/builders/base.js +41 -0
  148. package/src/mesh/builders/circle.js +63 -0
  149. package/src/mesh/builders/cuboid.js +135 -0
  150. package/src/mesh/builders/cylinder.js +131 -0
  151. package/src/mesh/builders/index.js +7 -0
  152. package/src/mesh/builders/plane.js +73 -0
  153. package/src/mesh/builders/utils.js +20 -0
  154. package/src/mesh/builders/uvsphere.js +80 -0
  155. package/src/mesh/builders/wireframe.js +62 -0
  156. package/src/mesh/index.js +4 -0
  157. package/src/mesh/mesh.js +149 -0
  158. package/src/objects/bone.js +17 -0
  159. package/src/objects/camera/camera.js +56 -0
  160. package/src/objects/camera/index.js +2 -0
  161. package/src/objects/camera/projection.js +203 -0
  162. package/src/objects/debug/index.js +1 -0
  163. package/src/objects/debug/skeleton.js +28 -0
  164. package/src/objects/index.js +7 -0
  165. package/src/objects/light/ambient.js +20 -0
  166. package/src/objects/light/directional.js +29 -0
  167. package/src/objects/light/index.js +5 -0
  168. package/src/objects/light/point.js +32 -0
  169. package/src/objects/light/shadow/index.js +1 -0
  170. package/src/objects/light/shadow/shadow.js +67 -0
  171. package/src/objects/light/spot.js +56 -0
  172. package/src/objects/mesh.js +141 -0
  173. package/src/objects/object3d.js +167 -0
  174. package/src/objects/skybox.js +38 -0
  175. package/src/plugins/camera/camera.js +19 -0
  176. package/src/plugins/camera/index.js +2 -0
  177. package/src/plugins/camera/nodes/cameraview.js +46 -0
  178. package/src/plugins/camera/nodes/index.js +2 -0
  179. package/src/plugins/camera/nodes/opaquepass.js +79 -0
  180. package/src/plugins/index.js +6 -0
  181. package/src/plugins/light/index.js +2 -0
  182. package/src/plugins/light/light.js +23 -0
  183. package/src/plugins/light/nodes/index.js +1 -0
  184. package/src/plugins/light/nodes/light.js +127 -0
  185. package/src/plugins/meshmaterial/index.js +3 -0
  186. package/src/plugins/meshmaterial/meshmaterial.js +381 -0
  187. package/src/plugins/meshmaterial/nodes/index.js +1 -0
  188. package/src/plugins/meshmaterial/nodes/meshmaterial.js +50 -0
  189. package/src/plugins/meshmaterial/resources/index.js +1 -0
  190. package/src/plugins/meshmaterial/resources/meshmaterialpipelines.js +50 -0
  191. package/src/plugins/shadow/index.js +3 -0
  192. package/src/plugins/shadow/nodes/index.js +3 -0
  193. package/src/plugins/shadow/nodes/shadow.js +272 -0
  194. package/src/plugins/shadow/nodes/shadowOccluder.js +112 -0
  195. package/src/plugins/shadow/nodes/shadowOpaquePass.js +73 -0
  196. package/src/plugins/shadow/resources/ShadowMap.js +99 -0
  197. package/src/plugins/shadow/resources/index.js +2 -0
  198. package/src/plugins/shadow/resources/shadowpipelines.js +25 -0
  199. package/src/plugins/shadow/shadow.js +31 -0
  200. package/src/plugins/skeletonhelper/index.js +1 -0
  201. package/src/plugins/skeletonhelper/skeletonhelper.js +160 -0
  202. package/src/plugins/skybox/index.js +3 -0
  203. package/src/plugins/skybox/nodes/index.js +1 -0
  204. package/src/plugins/skybox/nodes/skybox.js +143 -0
  205. package/src/plugins/skybox/resources/index.js +2 -0
  206. package/src/plugins/skybox/resources/skyboxmesh.js +14 -0
  207. package/src/plugins/skybox/resources/skyboxpipeline.js +6 -0
  208. package/src/plugins/skybox/skybox.js +137 -0
  209. package/src/renderer/core/index.js +179 -0
  210. package/src/renderer/graph/index.js +3 -0
  211. package/src/renderer/graph/nodes.js +34 -0
  212. package/src/renderer/graph/rendergraph.js +182 -0
  213. package/src/renderer/index.js +5 -0
  214. package/src/renderer/plugin.js +36 -0
  215. package/src/renderer/renderer.js +179 -0
  216. package/src/renderer/views.js +28 -0
  217. package/src/rendertarget/canvastarget.js +30 -0
  218. package/src/rendertarget/image.js +132 -0
  219. package/src/rendertarget/index.js +3 -0
  220. package/src/rendertarget/rendertarget.js +89 -0
  221. package/src/shader/basicFragment.glsl +30 -0
  222. package/src/shader/basicVertex.glsl +87 -0
  223. package/src/shader/common/color.glsl +7 -0
  224. package/src/shader/common/common.glsl +25 -0
  225. package/src/shader/common/index.js +4 -0
  226. package/src/shader/common/light.glsl +437 -0
  227. package/src/shader/common/math.glsl +12 -0
  228. package/src/shader/debug/index.js +2 -0
  229. package/src/shader/debug/skeletonFragment.glsl +8 -0
  230. package/src/shader/debug/skeletonVertex.glsl +27 -0
  231. package/src/shader/depthFragment.glsl +37 -0
  232. package/src/shader/index.js +11 -0
  233. package/src/shader/lambertFragment.glsl +126 -0
  234. package/src/shader/normalFragment.glsl +25 -0
  235. package/src/shader/phongFragment.glsl +140 -0
  236. package/src/shader/skyboxFragment.glsl +16 -0
  237. package/src/shader/skyboxVertex.glsl +20 -0
  238. package/src/shader/standardFragment.glsl +274 -0
  239. package/src/texture/index.js +2 -0
  240. package/src/texture/sampler.js +111 -0
  241. package/src/texture/texture.js +234 -0
  242. package/src/utils/index.js +115 -0
  243. package/tsconfig.json +11 -0
  244. package/website/config/index.ts +1 -0
  245. package/website/config/navigation.ts +53 -0
  246. package/website/content.config.ts +92 -0
  247. package/website/layouts/DocLayout.astro +501 -0
  248. package/website/layouts/Example.astro +91 -0
  249. package/website/pages/examples/[...slug].astro +77 -0
  250. package/website/pages/examples/index.astro +98 -0
  251. package/website/pages/examples/samples/[...slug].astro +17 -0
  252. package/website/pages/guide/[slug].astro +30 -0
  253. package/website/pages/guide/index.astro +21 -0
  254. package/website/pages/index.astro +9 -0
  255. package/website/plugins/remark-link-base.js +23 -0
  256. package/website/utils/url.ts +30 -0
@@ -0,0 +1,80 @@
1
+ import { Attribute } from "../attribute/index.js";
2
+ import { SeparateAttributeData } from "../attributedata/index.js";
3
+ import { Mesh } from "../mesh.js";
4
+ import { MeshBuilder } from "./base.js";
5
+
6
+ export class UVSphereMeshBuilder extends MeshBuilder {
7
+ /**
8
+ * @type {number}
9
+ */
10
+ radius = 0.5
11
+ /**
12
+ * @type {number}
13
+ */
14
+ longitudeSegments = 32
15
+ /**
16
+ * @type {number}
17
+ */
18
+ latitudeSegments = 32
19
+ /**
20
+ * @override
21
+ */
22
+ build() {
23
+ const vertices = [];
24
+ const normals = [];
25
+ const uvs = [];
26
+ const indices = [];
27
+
28
+ for (let i = 0; i <= this.latitudeSegments; i++) {
29
+ const phi = Math.PI * (-0.5 + (i / this.latitudeSegments));
30
+ const cosphi = Math.cos(phi)
31
+ const sinphi = Math.sin(phi)
32
+
33
+ for (let j = 0; j <= this.longitudeSegments; j++) {
34
+ const theta = 2 * Math.PI * (j / this.longitudeSegments);
35
+ const costheta = Math.cos(theta)
36
+ const sintheta = Math.sin(theta)
37
+
38
+ const x = this.radius * costheta * cosphi
39
+ const y = this.radius * sintheta * cosphi
40
+ const z = this.radius * sinphi
41
+ const nx = costheta * cosphi
42
+ const ny = sintheta * cosphi;
43
+ const nz = sinphi;
44
+ const u = 1.0 - (j / this.longitudeSegments);
45
+ const v = 1.0 - (i / this.latitudeSegments);
46
+
47
+ vertices.push(x, y, z);
48
+ normals.push(nx, ny, nz);
49
+ uvs.push(u, v);
50
+ }
51
+ }
52
+
53
+ for (let i = 0; i < this.latitudeSegments; i++) {
54
+ for (let j = 0; j < this.longitudeSegments; j++) {
55
+ const p0 = i * (this.longitudeSegments + 1) + j;
56
+ const p1 = p0 + this.longitudeSegments + 1;
57
+ indices.push(p0, p0 + 1, p1, p1, p0 + 1, p1 + 1);
58
+ }
59
+ }
60
+ const attributes = new SeparateAttributeData()
61
+
62
+ attributes
63
+ .set(
64
+ Attribute.Position.name,
65
+ new DataView(new Float32Array(vertices).buffer)
66
+ )
67
+ .set(
68
+ Attribute.Normal.name,
69
+ new DataView(new Float32Array(normals).buffer)
70
+ )
71
+ .set(
72
+ Attribute.UV.name,
73
+ new DataView(new Float32Array(uvs).buffer)
74
+ )
75
+ const mesh = new Mesh(attributes)
76
+ mesh.indices = new Uint16Array(indices)
77
+
78
+ return mesh
79
+ }
80
+ }
@@ -0,0 +1,62 @@
1
+ import { assert } from "../../utils/index.js"
2
+ import { Attribute } from "../attribute/index.js"
3
+ import { PrimitiveTopology } from "../../constants/index.js"
4
+ import { Mesh } from "../mesh.js"
5
+
6
+ export class WireframeBuilder {
7
+ mesh
8
+ /**
9
+ * @param {Mesh} mesh
10
+ */
11
+ constructor(mesh) {
12
+ this.mesh = mesh
13
+ }
14
+ build() {
15
+ const { indices, attributes } = this.mesh
16
+ const edges = new Set()
17
+ /**@type {number[]} */
18
+ const lineIndices = []
19
+
20
+ if (indices) {
21
+ // Triangle list
22
+ for (let i = 0; i < indices.length; i += 3) {
23
+ const a = /**@type {number}*/ (indices[i]),
24
+ b = /**@type {number}*/ (indices[i + 1]),
25
+ c = /**@type {number}*/ (indices[i + 2]);
26
+ addEdge(edges, lineIndices, a, b);
27
+ addEdge(edges, lineIndices, b, c);
28
+ addEdge(edges, lineIndices, c, a);
29
+ }
30
+ } else {
31
+ const position = attributes.get(Attribute.Position.name)
32
+
33
+ assert(position, "No position data found")
34
+ for (let i = 0; i < position.byteLength / (4 * 3); i += 3) {
35
+ addEdge(edges, lineIndices, i, i + 1);
36
+ addEdge(edges, lineIndices, i + 1, i + 2);
37
+ addEdge(edges, lineIndices, i + 2, i);
38
+ }
39
+ }
40
+
41
+ const mesh = new Mesh(this.mesh.attributes)
42
+ mesh.indices = new Uint32Array(lineIndices)
43
+ mesh.topology = PrimitiveTopology.Lines
44
+
45
+ return mesh
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Adds an edge to the list, avoiding duplicates.
51
+ * @param {Set<string>} edgeMap
52
+ * @param {number[]} out
53
+ * @param {number} a
54
+ * @param {number} b
55
+ */
56
+ function addEdge(edgeMap, out, a, b) {
57
+ const key = a < b ? `${a}_${b}` : `${b}_${a}`;
58
+ if (!edgeMap.has(key)) {
59
+ edgeMap.add(key);
60
+ out.push(a, b);
61
+ }
62
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./attribute/index.js"
2
+ export * from "./attributedata/index.js"
3
+ export * from "./builders/index.js"
4
+ export * from "./mesh.js"
@@ -0,0 +1,149 @@
1
+ import { PrimitiveTopology } from "../constants/index.js"
2
+ import { Attribute } from "./attribute/index.js"
3
+ import { SeparateAttributeData } from "./attributedata/separate.js"
4
+ import { Affine3 } from "../math/index.js"
5
+
6
+ export class Mesh {
7
+ /**
8
+ * @type {Uint8Array | Uint16Array | Uint32Array | undefined}
9
+ */
10
+ #indices
11
+
12
+ /**
13
+ * @type {SeparateAttributeData}
14
+ */
15
+ #attributes
16
+
17
+ /**
18
+ * @type {PrimitiveTopology}
19
+ */
20
+ topology = PrimitiveTopology.Triangles
21
+
22
+ /**
23
+ * Tracks if the attribute data has changed since last checked.
24
+ * @type {boolean}
25
+ * */
26
+ #changed = false
27
+
28
+ /**
29
+ * @param {SeparateAttributeData} attributes
30
+ */
31
+ constructor(attributes) {
32
+ this.#attributes = attributes
33
+ }
34
+
35
+ /**
36
+ * @package
37
+ * @returns {boolean}
38
+ * Indicates if the data has changed since last queried.
39
+ * Automatically resets the flag.
40
+ */
41
+ get changed() {
42
+ const wasChanged = this.#changed
43
+ this.#changed = false
44
+ return wasChanged || this.attributes.changed
45
+ }
46
+
47
+ /**
48
+ * @type {Uint8Array | Uint16Array | Uint32Array | undefined}
49
+ */
50
+ get indices() {
51
+ return this.#indices
52
+ }
53
+ set indices(value) {
54
+ this.#indices = value
55
+ this.#changed = true
56
+ }
57
+
58
+ /**
59
+ * @type {SeparateAttributeData}
60
+ */
61
+ get attributes() {
62
+ return this.#attributes
63
+ }
64
+ set attributes(value) {
65
+ this.#attributes = value
66
+ this.#changed = true
67
+ }
68
+
69
+ /**
70
+ * @param {Affine3} affine
71
+ */
72
+ transform(affine) {
73
+ this.attributes.transform(affine)
74
+ return this
75
+ }
76
+
77
+ /**
78
+ * @param {Mesh} other
79
+ */
80
+ merge(other) {
81
+ const newAttributes = this.attributes.merge(other.attributes)
82
+ const newMesh = new Mesh(newAttributes)
83
+
84
+ if (this.indices && other.indices) {
85
+ const positions = this.attributes.get(Attribute.Position.name)
86
+
87
+ if (!positions) {
88
+ return newMesh
89
+ }
90
+
91
+ // TODO: Remove this hardcoding and use the attribute map instead
92
+ const attributeCount = positions.byteLength / (3 * 4)
93
+ const offset = this.indices.length
94
+
95
+ const newIndices = new Uint16Array(this.indices.length + other.indices.length)
96
+
97
+ for (let i = 0; i < this.indices.length; i++) {
98
+ const index = /**@type {number} */(this.indices[i]);
99
+
100
+ newIndices[i] = index
101
+ }
102
+
103
+ for (let i = 0; i < other.indices.length; i++) {
104
+ const index = /**@type {number} */(other.indices[i]);
105
+
106
+ newIndices[i + offset] = index + attributeCount
107
+ }
108
+ newMesh.indices = newIndices
109
+ } else if (!this.indices && !this.indices) {
110
+ // Do nothing because attributes are already merged
111
+ } else {
112
+ // TODO: How do we merge an indexed and non-indexed mesh?
113
+ throw "Invalid merge, both meshes must either have indices or not have them."
114
+ }
115
+ return newMesh
116
+ }
117
+
118
+ normalizeJointWeights() {
119
+ const weights = this.attributes.get(Attribute.JointWeight.name)
120
+
121
+ if (!weights) return
122
+
123
+ const data = new Float32Array(
124
+ weights.buffer,
125
+ weights.byteOffset,
126
+ weights.byteLength / Float32Array.BYTES_PER_ELEMENT
127
+ )
128
+
129
+ for (let i = 0; i < data.length; i += 4) {
130
+ const sum = /**@type {number}*/(data[i]) +
131
+ /**@type {number}*/ (data[i + 1]) +
132
+ /**@type {number}*/ (data[i + 2]) +
133
+ /**@type {number}*/ (data[i + 3])
134
+
135
+ if (sum === 0) {
136
+ data[i] = 0
137
+ data[i + 1] = 0
138
+ data[i + 2] = 0
139
+ data[i + 3] = 0
140
+ } else {
141
+ const inv = 1 / sum
142
+ data[i] = /**@type {number}*/(data[i]) * inv
143
+ data[i + 1] = /**@type {number}*/ (data[i + 1]) * inv
144
+ data[i + 2] = /**@type {number}*/(data[i + 2]) * inv
145
+ data[i + 3] = /**@type {number}*/(data[i + 3]) * inv
146
+ }
147
+ }
148
+ }
149
+ }
@@ -0,0 +1,17 @@
1
+ import { Object3D } from "./object3d.js";
2
+
3
+ export class Bone3D extends Object3D {
4
+ index = -1
5
+
6
+ /**
7
+ * @override
8
+ * @param {Map<Object3D, Object3D>} [entityMap]
9
+ */
10
+ clone(entityMap){
11
+ const clone = super.clone(entityMap)
12
+
13
+ clone.index = this.index
14
+
15
+ return clone
16
+ }
17
+ }
@@ -0,0 +1,56 @@
1
+ import { Affine3, Matrix4 } from "../../math/index.js"
2
+ import { Object3D } from "../object3d.js"
3
+ import { RenderTarget } from "../../rendertarget/index.js"
4
+ import { PerspectiveProjection, Projection } from "./projection.js"
5
+
6
+ export class Camera extends Object3D {
7
+ near = 0.1
8
+
9
+ far = 2000
10
+
11
+ /**
12
+ * @type {RenderTarget}
13
+ */
14
+ target
15
+ /**
16
+ * @type {Projection}
17
+ */
18
+ projection = new PerspectiveProjection()
19
+
20
+ /**
21
+ * @type {Matrix4}
22
+ */
23
+ view = new Matrix4()
24
+
25
+ /**
26
+ * @param {RenderTarget} target
27
+ */
28
+ constructor(target){
29
+ super()
30
+ this.target = target
31
+ }
32
+ /**
33
+ * @override
34
+ */
35
+ update() {
36
+ super.update()
37
+ const inverseTransform = Affine3.toMatrix4(
38
+ Affine3.invert(this.transform.world)
39
+ )
40
+ this.view.copy(inverseTransform)
41
+ }
42
+
43
+ getData() {
44
+ const { near, far } = this
45
+ return {
46
+ name: "CameraBlock",
47
+ data: new Float32Array([
48
+ ...this.view,
49
+ ...this.projection.asProjectionMatrix(near, far),
50
+ ...this.transform.position,
51
+ this.near,
52
+ this.far
53
+ ]).buffer
54
+ }
55
+ }
56
+ }
@@ -0,0 +1,2 @@
1
+ export * from './camera.js'
2
+ export * from './projection.js'
@@ -0,0 +1,203 @@
1
+ import { Matrix4 } from '../../math/index.js'
2
+
3
+ export class Projection {
4
+
5
+ /**
6
+ * @returns {Matrix4}
7
+ * @param {number} _near
8
+ * @param {number} _far
9
+ */
10
+ asProjectionMatrix(_near, _far) {
11
+ return new Matrix4()
12
+ }
13
+
14
+ /**
15
+ * @returns {boolean}
16
+ */
17
+ isPerspective() {
18
+ return false
19
+ }
20
+
21
+ /**
22
+ * @returns {boolean}
23
+ */
24
+ isOrthographic() {
25
+ return false
26
+ }
27
+ }
28
+
29
+ export class PerspectiveProjection extends Projection {
30
+
31
+ /**
32
+ * @readonly
33
+ * @type {ProjectionType}
34
+ */
35
+ type = ProjectionType.Perspective
36
+
37
+ /**
38
+ * @type {number}
39
+ */
40
+ fov
41
+
42
+ /**
43
+ * @type {number}
44
+ */
45
+ aspect
46
+
47
+ /**
48
+ * @param {number} fov
49
+ * @param {number} aspect
50
+ */
51
+ constructor(fov = Math.PI / 2, aspect = 1) {
52
+ super()
53
+ this.fov = fov
54
+ this.aspect = aspect
55
+ }
56
+
57
+ /**
58
+ * @override
59
+ */
60
+ isPerspective() {
61
+ return true
62
+ }
63
+
64
+ /**
65
+ * @param {number} near
66
+ * @param {number} far
67
+ * @returns {Matrix4}
68
+ * @override
69
+ */
70
+ asProjectionMatrix(near, far) {
71
+ const top = near * Math.tan(0.5 * this.fov)
72
+ const height = 2 * top
73
+ const width = this.aspect * height
74
+ const left = -0.5 * width
75
+
76
+ return PerspectiveProjection.from(left, left + width, top, top - height, near, far)
77
+ }
78
+
79
+ /**
80
+ * @param {number} left
81
+ * @param {number} right
82
+ * @param {number} top
83
+ * @param {number} bottom
84
+ * @param {number} near
85
+ * @param {number} far
86
+ * @param {Matrix4} out
87
+ */
88
+ static from(left, right, top, bottom, near, far, out = new Matrix4()) {
89
+ const x = 2 * near / (right - left)
90
+ const y = 2 * near / (top - bottom)
91
+
92
+ const a = (right + left) / (right - left)
93
+ const b = (top + bottom) / (top - bottom)
94
+
95
+ const c = -(far + near) / (far - near)
96
+ const d = (-2 * far * near) / (far - near)
97
+
98
+ out.a = x
99
+ out.b = 0
100
+ out.c = 0
101
+ out.d = 0
102
+ out.e = 0
103
+ out.f = y
104
+ out.g = 0
105
+ out.h = 0
106
+ out.i = a
107
+ out.j = b
108
+ out.k = c
109
+ out.l = -1
110
+ out.m = 0
111
+ out.n = 0
112
+ out.o = d
113
+ out.p = 0
114
+
115
+ return out
116
+ }
117
+ }
118
+
119
+ export class OrthographicProjection extends Projection {
120
+
121
+ /**
122
+ * @readonly
123
+ * @type {ProjectionType}
124
+ */
125
+ type = ProjectionType.Orthographic
126
+ constructor(left = -1, right = 1, top = 1, bottom = -1) {
127
+ super()
128
+ this.left = left
129
+ this.right = right
130
+ this.top = top
131
+ this.bottom = bottom
132
+ }
133
+
134
+ /**
135
+ * @override
136
+ */
137
+ isOrthographic() {
138
+ return true
139
+ }
140
+
141
+ /**
142
+ * @param {number} near
143
+ * @param {number} far
144
+ * @returns {Matrix4}
145
+ * @override
146
+ */
147
+ asProjectionMatrix(near, far) {
148
+ return OrthographicProjection.from(this.left, this.right, this.top, this.bottom, near, far)
149
+ }
150
+
151
+ /**
152
+ * @param {number} left
153
+ * @param {number} right
154
+ * @param {number} top
155
+ * @param {number} bottom
156
+ * @param {number} near
157
+ * @param {number} far
158
+ * @param {Matrix4} out
159
+ */
160
+ static from(left, right, top, bottom, near, far, out = new Matrix4()) {
161
+
162
+ const w = 1.0 / (right - left)
163
+ const h = 1.0 / (top - bottom)
164
+ const p = 1.0 / (far - near)
165
+
166
+ const x = (right + left) * w
167
+ const y = (top + bottom) * h
168
+
169
+ const z = (far + near) * p
170
+ const zInv = -2 * p
171
+
172
+ out.a = 2 * w
173
+ out.b = 0
174
+ out.c = 0
175
+ out.d = 0
176
+
177
+ out.e = 0
178
+ out.f = 2 * h
179
+ out.g = 0
180
+ out.h = 0
181
+
182
+ out.i = 0
183
+ out.j = 0
184
+ out.k = zInv
185
+ out.l = 0
186
+
187
+ out.m = -x
188
+ out.n = -y
189
+ out.o = -z
190
+ out.p = 1
191
+
192
+ return out
193
+ }
194
+ }
195
+
196
+ /**
197
+ * @readonly
198
+ * @enum {number}
199
+ */
200
+ export const ProjectionType = /**@type {const}*/({
201
+ Perspective: 0,
202
+ Orthographic: 1
203
+ })
@@ -0,0 +1 @@
1
+ export * from './skeleton.js'
@@ -0,0 +1,28 @@
1
+ import { Bone3D } from "../bone.js";
2
+ import { MeshMaterial3D } from "../mesh.js";
3
+ import { Object3D } from "../object3d.js";
4
+
5
+ /**
6
+ * @class
7
+ */
8
+ export class SkeletonHelper extends Object3D {
9
+ /**
10
+ * @type {Bone3D}
11
+ */
12
+ rootBone
13
+
14
+ /**
15
+ * @type {MeshMaterial3D}
16
+ */
17
+ skinnedMesh
18
+ /**
19
+ * @param {Bone3D} rootBone
20
+ * @param {MeshMaterial3D} skinnedMesh
21
+ */
22
+ constructor(rootBone, skinnedMesh) {
23
+ super()
24
+ this.rootBone = rootBone
25
+ this.skinnedMesh = skinnedMesh
26
+ }
27
+ }
28
+
@@ -0,0 +1,7 @@
1
+ export * from './camera/index.js'
2
+ export * from './light/index.js'
3
+ export * from './debug/index.js'
4
+ export * from './object3d.js'
5
+ export * from './mesh.js'
6
+ export * from './skybox.js'
7
+ export * from './bone.js'
@@ -0,0 +1,20 @@
1
+ import { Color } from '../../math/index.js'
2
+ import { Object3D } from '../object3d.js'
3
+
4
+ export class AmbientLight extends Object3D {
5
+ intensity = 1
6
+ color = new Color(1, 1, 1)
7
+
8
+ getData() {
9
+ return {
10
+ name: "AmbientLightBlock",
11
+ data: new Float32Array([
12
+ this.intensity,
13
+ 0,
14
+ 0,
15
+ 0,
16
+ ...this.color
17
+ ]).buffer
18
+ }
19
+ }
20
+ }
@@ -0,0 +1,29 @@
1
+ import { Color, Vector3 } from '../../math/index.js'
2
+ import { Object3D } from '../object3d.js'
3
+ import { OrthographicShadow } from './shadow/index.js'
4
+
5
+ export class DirectionalLight extends Object3D {
6
+ intensity = 1.0
7
+ color = new Color()
8
+
9
+ /**
10
+ * @type {OrthographicShadow | undefined}
11
+ */
12
+ shadow
13
+
14
+ /**
15
+ */
16
+ pack() {
17
+ const direction = this.transform.world.transformWithoutTranslation(new Vector3(0, 0, -1))
18
+
19
+ return [
20
+ ...this.color,
21
+ ...direction,
22
+ this.intensity,
23
+ 0,
24
+ 0,
25
+ 0,
26
+ 0
27
+ ]
28
+ }
29
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./ambient.js"
2
+ export * from "./directional.js"
3
+ export * from "./spot.js"
4
+ export * from "./point.js"
5
+ export * from "./shadow/index.js"
@@ -0,0 +1,32 @@
1
+ import { Color, Vector3 } from '../../math/index.js'
2
+ import { Object3D } from '../object3d.js'
3
+ import { SpotLightShadow } from './shadow/index.js'
4
+
5
+ export class PointLight extends Object3D {
6
+ color = new Color()
7
+ radius = 1.0
8
+ decay = 2
9
+ intensity = 1.0
10
+
11
+ /**
12
+ * @type {SpotLightShadow | undefined}
13
+ */
14
+ shadow
15
+
16
+ pack() {
17
+ const worldPosition = new Vector3(
18
+ this.transform.world.x,
19
+ this.transform.world.y,
20
+ this.transform.world.z
21
+ )
22
+ return [
23
+ ...this.color,
24
+ ...worldPosition,
25
+ this.intensity,
26
+ Math.max(0, this.radius),
27
+ this.decay,
28
+ 0,
29
+ 0
30
+ ]
31
+ }
32
+ }