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,235 @@
1
+ /**@import { WebGLBindGroupDescriptor, WebGLBindGroupLayoutDescriptor, WebGLBufferDescriptor, WebGLRenderPipelineDescriptor, WebGLTextureDescriptor, WebGLWriteTextureDescriptor } from './descriptors.js' */
2
+ import { CullFace, FrontFaceDirection, TextureFormat, TextureType, getTextureFormatSize } from "../../constants/index.js"
3
+ import { assert } from "../../utils/index.js"
4
+ import { getFramebufferAttachment, getWebGLTextureFormat, mapWebGLAttachmentToBufferBit } from "../../function.js"
5
+ import { WebGLExtensions } from "../extensions.js"
6
+ import { WebGLBindGroup, WebGLBindGroupLayout } from "./bindgroup.js"
7
+ import { WebGLRenderPipeline } from "./renderpipeline.js"
8
+ import { WebGLRenderPassEncoder } from "./renderpassencoder.js"
9
+ import { GPUBuffer, GPUTexture } from "../resources/index.js"
10
+ import { allocateTexture2D, allocateCubemap, allocateTexture2DArray, updateTexture2D, updateCubeMap, updateTexture2DArray, createProgramFromSrc } from "./utils.js"
11
+ import { CompareFunction } from "../constants.js"
12
+
13
+ export class WebGLRenderDevice {
14
+ /**
15
+ * @private
16
+ * @type {WebGLFramebuffer}
17
+ */
18
+ drawBuffer
19
+ /**
20
+ * @private
21
+ * @type {WebGLFramebuffer}
22
+ */
23
+ readBuffer
24
+ /**
25
+ * @readonly
26
+ * @type {HTMLCanvasElement}
27
+ */
28
+ canvas
29
+
30
+ /**
31
+ * @readonly
32
+ * @type {WebGLExtensions}
33
+ */
34
+ extensions
35
+
36
+ /**
37
+ * @readonly
38
+ * @type {WebGL2RenderingContext}
39
+ */
40
+ context
41
+ /**
42
+ * @param {HTMLCanvasElement} [canvas]
43
+ * @param {WebGLContextAttributes} [options]
44
+ */
45
+ constructor(canvas, options) {
46
+ this.canvas = canvas || document.createElement('canvas')
47
+ const context = this.canvas.getContext('webgl2', options)
48
+
49
+ assert(context, "Webgl context creation failed")
50
+
51
+ this.drawBuffer = context.createFramebuffer()
52
+ this.readBuffer = context.createFramebuffer()
53
+ this.context = context
54
+ this.extensions = new WebGLExtensions(this.context)
55
+ this.extensions.get("OES_texture_float_linear")
56
+ }
57
+
58
+ /**
59
+ *
60
+ * @param {WebGLRenderPipelineDescriptor} descriptor
61
+ */
62
+ createRenderPipeline(descriptor) {
63
+ const programInfo = createProgramFromSrc(
64
+ this.context,
65
+ descriptor.vertex.compile(),
66
+ descriptor.fragment?.source?.compile() || noopFragment,
67
+ descriptor.vertexLayout
68
+ )
69
+
70
+ assert(programInfo, 'Cannot create webgl render pipeline')
71
+
72
+ return new WebGLRenderPipeline({
73
+ program: programInfo.program,
74
+ uniforms: programInfo.uniforms,
75
+ uniformBlocks: programInfo.uniformBlocks,
76
+ vertexLayout: descriptor.vertexLayout,
77
+ topology: descriptor.topology,
78
+ targets: descriptor.fragment?.targets || [],
79
+ frontFace: descriptor.frontFace ?? FrontFaceDirection.CCW,
80
+ cullFace: descriptor.cullFace ?? CullFace.Back,
81
+ depthCompare: descriptor.depthCompare ?? CompareFunction.Less,
82
+ depthWrite: descriptor.depthWrite ?? true
83
+ })
84
+ }
85
+
86
+ /**
87
+ * @param {WebGLBindGroupLayoutDescriptor} descriptor
88
+ */
89
+ createBindGroupLayout(descriptor) {
90
+ return new WebGLBindGroupLayout(descriptor)
91
+ }
92
+
93
+ /**
94
+ * @param {WebGLBindGroupDescriptor} descriptor
95
+ */
96
+ createBindGroup(descriptor) {
97
+ return new WebGLBindGroup(descriptor)
98
+ }
99
+
100
+ /**
101
+ * @param {any} [_descriptor]
102
+ */
103
+ beginRenderPass(_descriptor) {
104
+ return new WebGLRenderPassEncoder(this.context)
105
+ }
106
+
107
+ /**
108
+ * @param {WebGLBufferDescriptor} descriptor
109
+ * @returns {GPUBuffer}
110
+ */
111
+ createBuffer({
112
+ size,
113
+ usage,
114
+ type
115
+ }) {
116
+ const { context } = this
117
+ const buffer = context.createBuffer()
118
+
119
+ context.bindBuffer(type, buffer)
120
+ context.bufferData(type, size, usage)
121
+
122
+ return new GPUBuffer(buffer, type, size)
123
+ }
124
+
125
+ /**
126
+ * @param {GPUBuffer} buffer
127
+ * @param {ArrayBufferView} data
128
+ * @param {number} bufferOffset
129
+ * @param {number} dataOffset
130
+ * @param {number} size
131
+ */
132
+ writeBuffer(buffer, data, bufferOffset = 0, dataOffset = 0, size = data.byteLength) {
133
+ const { context } = this
134
+ const dataView = new DataView(data.buffer, data.byteOffset, data.byteLength)
135
+
136
+ context.bindBuffer(buffer.type, buffer.inner)
137
+ context.bufferSubData(buffer.type, bufferOffset, dataView, dataOffset, size)
138
+ }
139
+
140
+ /**
141
+ * @param {WebGLTextureDescriptor} descriptor
142
+ * @returns {GPUTexture}
143
+ */
144
+ createTexture(descriptor) {
145
+ const { width, height, depth = 1, type, format } = descriptor
146
+ const { context } = this
147
+ const texture = context.createTexture()
148
+ const form = getWebGLTextureFormat(format)
149
+
150
+ assert(form, "Invalid texture format")
151
+
152
+ context.bindTexture(type, texture)
153
+ switch (type) {
154
+ case TextureType.Texture2D:
155
+ allocateTexture2D(context, descriptor, form)
156
+ break
157
+ case TextureType.TextureCubeMap:
158
+ allocateCubemap(context, descriptor, form)
159
+ break
160
+ case TextureType.Texture2DArray:
161
+ allocateTexture2DArray(context, descriptor, form)
162
+ break
163
+ default:
164
+ throw "The texture type is not supported."
165
+ }
166
+ const pixelSize = getTextureFormatSize(format)
167
+ return new GPUTexture(texture, type, form, format, width, height, depth, pixelSize)
168
+ }
169
+
170
+ /**
171
+ * @param {WebGLWriteTextureDescriptor} descriptor
172
+ */
173
+ writeTexture(descriptor) {
174
+ const { texture } = descriptor
175
+ const { context } = this
176
+
177
+ context.bindTexture(texture.type, texture.inner)
178
+ switch (texture.type) {
179
+ case TextureType.Texture2D:
180
+ updateTexture2D(context, descriptor)
181
+ break;
182
+ case TextureType.TextureCubeMap:
183
+ updateCubeMap(context, descriptor)
184
+ break
185
+ case TextureType.Texture2DArray:
186
+ updateTexture2DArray(context, descriptor)
187
+ break
188
+ default:
189
+ throw "Unsupported texture type."
190
+ }
191
+ }
192
+
193
+ /**
194
+ * @param {WebGLRenderbuffer} source
195
+ * @param {TextureFormat} sourceFormat
196
+ * @param {GPUTexture} destination
197
+ */
198
+ copyRenderBufferToTexture(source, sourceFormat, destination) {
199
+ const { context } = this
200
+ const srcAttachment = getFramebufferAttachment(sourceFormat)
201
+ const dstAttachment = getFramebufferAttachment(destination.actualFormat)
202
+
203
+ assert(srcAttachment === dstAttachment ? {} : undefined, "Textures need to bind to same attachment to be copy to each other")
204
+
205
+ context.bindFramebuffer(WebGL2RenderingContext.DRAW_FRAMEBUFFER, this.drawBuffer)
206
+ context.bindFramebuffer(WebGL2RenderingContext.READ_FRAMEBUFFER, this.readBuffer)
207
+
208
+ context.framebufferRenderbuffer(
209
+ WebGL2RenderingContext.READ_FRAMEBUFFER,
210
+ srcAttachment,
211
+ WebGL2RenderingContext.RENDERBUFFER,
212
+ source
213
+ )
214
+ context.framebufferTexture2D(
215
+ WebGL2RenderingContext.DRAW_FRAMEBUFFER,
216
+ dstAttachment,
217
+ WebGL2RenderingContext.TEXTURE_2D,
218
+ destination.inner,
219
+ 0
220
+ )
221
+
222
+ context.blitFramebuffer(
223
+ 0, 0, destination.width, destination.height,
224
+ 0, 0, destination.width, destination.height,
225
+ mapWebGLAttachmentToBufferBit(dstAttachment),
226
+ WebGL2RenderingContext.NEAREST
227
+ )
228
+ }
229
+ }
230
+
231
+ const noopFragment = `#version 300 es
232
+ precision mediump float;
233
+
234
+ void main(){ }
235
+ `
@@ -0,0 +1,358 @@
1
+ import {
2
+ GlDataType,
3
+ VertexFormat,
4
+ TextureFormat
5
+ } from "./constants/index.js"
6
+
7
+ // TODO: Use dataview instead of this
8
+ /**
9
+ * Converts an ArrayBuffer to a corresponding TypedArray based on `GlDataType`.
10
+ *
11
+ * @param {ArrayBufferLike} buffer - The buffer to convert.
12
+ * @param {GlDataType} dataType - One of the values from GlDataType.
13
+ * @throws {Error} If `dataType` is unknown.
14
+ */
15
+ export function convertBufferToTypedArray(
16
+ buffer,
17
+ dataType,
18
+ offset = 0,
19
+ length = buffer.byteLength
20
+ ) {
21
+ switch (dataType) {
22
+ case GlDataType.Float:
23
+ return new Float32Array(buffer, offset, length / Float32Array.BYTES_PER_ELEMENT);
24
+ case GlDataType.UnsignedInt:
25
+ return new Uint32Array(buffer, offset, length / Uint32Array.BYTES_PER_ELEMENT);
26
+ case GlDataType.Int:
27
+ return new Int32Array(buffer, offset, length / Int32Array.BYTES_PER_ELEMENT);
28
+ case GlDataType.UnsignedShort:
29
+ return new Uint16Array(buffer, offset, length / Uint16Array.BYTES_PER_ELEMENT);
30
+ case GlDataType.Short:
31
+ return new Int16Array(buffer, offset, length / Int16Array.BYTES_PER_ELEMENT);
32
+ case GlDataType.UnsignedByte:
33
+ return new Uint8Array(buffer, offset, length / Uint8Array.BYTES_PER_ELEMENT);
34
+ case GlDataType.Byte:
35
+ return new Int8Array(buffer, offset, length / Int8Array.BYTES_PER_ELEMENT);
36
+ default:
37
+ throw new Error(`Unsupported GL data type: 0x${dataType.toString(16)}`);
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Returns a WebGL texture format configuration based on a custom texture format enum.
43
+ *
44
+ * @param {number} format - The custom TextureFormat enum value.
45
+ * @returns {WebGLTextureFormat | undefined} The corresponding texture format, or undefined if not supported.
46
+ */
47
+ export function getWebGLTextureFormat(format) {
48
+ const gl = WebGL2RenderingContext
49
+ switch (format) {
50
+ // --- 8-bit ---
51
+ case TextureFormat.R8Unorm:
52
+ return new WebGLTextureFormat(gl.R8, gl.RED, gl.UNSIGNED_BYTE);
53
+ case TextureFormat.R8Snorm:
54
+ return new WebGLTextureFormat(gl.R8_SNORM, gl.RED, gl.BYTE);
55
+ case TextureFormat.R8Uint:
56
+ return new WebGLTextureFormat(gl.R8UI, gl.RED_INTEGER, gl.UNSIGNED_BYTE);
57
+ case TextureFormat.R8Sint:
58
+ return new WebGLTextureFormat(gl.R8I, gl.RED_INTEGER, gl.BYTE);
59
+
60
+ // --- 16-bit ---
61
+ case TextureFormat.R16Uint:
62
+ return new WebGLTextureFormat(gl.R16UI, gl.RED_INTEGER, gl.UNSIGNED_SHORT);
63
+ case TextureFormat.R16Sint:
64
+ return new WebGLTextureFormat(gl.R16I, gl.RED_INTEGER, gl.SHORT);
65
+ case TextureFormat.R16Float:
66
+ return new WebGLTextureFormat(gl.R16F, gl.RED, gl.HALF_FLOAT);
67
+ case TextureFormat.RG8Unorm:
68
+ return new WebGLTextureFormat(gl.RG8, gl.RG, gl.UNSIGNED_BYTE);
69
+ case TextureFormat.RG8Snorm:
70
+ return new WebGLTextureFormat(gl.RG8_SNORM, gl.RG, gl.BYTE);
71
+ case TextureFormat.RG8Uint:
72
+ return new WebGLTextureFormat(gl.RG8UI, gl.RG_INTEGER, gl.UNSIGNED_BYTE);
73
+ case TextureFormat.RG8Sint:
74
+ return new WebGLTextureFormat(gl.RG8I, gl.RG_INTEGER, gl.BYTE);
75
+
76
+ // --- 32-bit ---
77
+ case TextureFormat.R32Uint:
78
+ return new WebGLTextureFormat(gl.R32UI, gl.RED_INTEGER, gl.UNSIGNED_INT);
79
+ case TextureFormat.R32Sint:
80
+ return new WebGLTextureFormat(gl.R32I, gl.RED_INTEGER, gl.INT);
81
+ case TextureFormat.R32Float:
82
+ return new WebGLTextureFormat(gl.R32F, gl.RED, gl.FLOAT);
83
+ case TextureFormat.RG16Uint:
84
+ return new WebGLTextureFormat(gl.RG16UI, gl.RG_INTEGER, gl.UNSIGNED_SHORT);
85
+ case TextureFormat.RG16Sint:
86
+ return new WebGLTextureFormat(gl.RG16I, gl.RG_INTEGER, gl.SHORT);
87
+ case TextureFormat.RG16Float:
88
+ return new WebGLTextureFormat(gl.RG16F, gl.RG, gl.HALF_FLOAT);
89
+ case TextureFormat.RGBA8Unorm:
90
+ return new WebGLTextureFormat(gl.RGBA8, gl.RGBA, gl.UNSIGNED_BYTE);
91
+ case TextureFormat.RGBA8UnormSRGB:
92
+ return new WebGLTextureFormat(gl.SRGB8_ALPHA8, gl.RGBA, gl.UNSIGNED_BYTE);
93
+ case TextureFormat.RGBA8Snorm:
94
+ return new WebGLTextureFormat(gl.RGBA8_SNORM, gl.RGBA, gl.BYTE);
95
+ case TextureFormat.RGBA8Uint:
96
+ return new WebGLTextureFormat(gl.RGBA8UI, gl.RGBA_INTEGER, gl.UNSIGNED_BYTE);
97
+ case TextureFormat.RGBA8Sint:
98
+ return new WebGLTextureFormat(gl.RGBA8I, gl.RGBA_INTEGER, gl.BYTE);
99
+
100
+ // --- 64-bit ---
101
+ case TextureFormat.RG32Uint:
102
+ return new WebGLTextureFormat(gl.RG32UI, gl.RG_INTEGER, gl.UNSIGNED_INT);
103
+ case TextureFormat.RG32Sint:
104
+ return new WebGLTextureFormat(gl.RG32I, gl.RG_INTEGER, gl.INT);
105
+ case TextureFormat.RG32Float:
106
+ return new WebGLTextureFormat(gl.RG32F, gl.RG, gl.FLOAT);
107
+ case TextureFormat.RGBA16Uint:
108
+ return new WebGLTextureFormat(gl.RGBA16UI, gl.RGBA_INTEGER, gl.UNSIGNED_SHORT);
109
+ case TextureFormat.RGBA16Sint:
110
+ return new WebGLTextureFormat(gl.RGBA16I, gl.RGBA_INTEGER, gl.SHORT);
111
+ case TextureFormat.RGBA16Float:
112
+ return new WebGLTextureFormat(gl.RGBA16F, gl.RGBA, gl.HALF_FLOAT);
113
+
114
+ // --- 128-bit ---
115
+ case TextureFormat.RGBA32Uint:
116
+ return new WebGLTextureFormat(gl.RGBA32UI, gl.RGBA_INTEGER, gl.UNSIGNED_INT);
117
+ case TextureFormat.RGBA32Sint:
118
+ return new WebGLTextureFormat(gl.RGBA32I, gl.RGBA_INTEGER, gl.INT);
119
+ case TextureFormat.RGBA32Float:
120
+ return new WebGLTextureFormat(gl.RGBA32F, gl.RGBA, gl.FLOAT);
121
+
122
+ // --- Depth / Stencil ---
123
+ case TextureFormat.Depth16Unorm:
124
+ return new WebGLTextureFormat(gl.DEPTH_COMPONENT16, gl.DEPTH_COMPONENT, gl.UNSIGNED_SHORT);
125
+ case TextureFormat.Depth24Plus:
126
+ return new WebGLTextureFormat(gl.DEPTH_COMPONENT24, gl.DEPTH_COMPONENT, gl.UNSIGNED_INT);
127
+ case TextureFormat.Depth32Float:
128
+ return new WebGLTextureFormat(gl.DEPTH_COMPONENT32F, gl.DEPTH_COMPONENT, gl.FLOAT);
129
+ case TextureFormat.Depth24PlusStencil8:
130
+ return new WebGLTextureFormat(gl.DEPTH24_STENCIL8, gl.DEPTH_STENCIL, gl.UNSIGNED_INT_24_8);
131
+ case TextureFormat.Depth32FloatStencil8:
132
+ return new WebGLTextureFormat(gl.DEPTH32F_STENCIL8, gl.DEPTH_STENCIL, gl.FLOAT_32_UNSIGNED_INT_24_8_REV);
133
+
134
+ default:
135
+ return undefined;
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Converts a TextureFormat enum value to the appropriate framebuffer attachment type.
141
+ * @param {number} format - A value from TextureFormat.
142
+ * @returns {number} A GL_* attachment enum, e.g. gl.COLOR_ATTACHMENT0, gl.DEPTH_ATTACHMENT, etc.
143
+ */
144
+ export function getFramebufferAttachment(format) {
145
+ const context = WebGL2RenderingContext;
146
+
147
+ switch (format) {
148
+ // --- Depth-only formats ---
149
+ case TextureFormat.Depth16Unorm:
150
+ case TextureFormat.Depth24Plus:
151
+ case TextureFormat.Depth32Float:
152
+ return context.DEPTH_ATTACHMENT;
153
+
154
+ // --- Stencil-only format ---
155
+ case TextureFormat.Stencil8:
156
+ return context.STENCIL_ATTACHMENT;
157
+
158
+ // --- Combined depth + stencil formats ---
159
+ case TextureFormat.Depth24PlusStencil8:
160
+ case TextureFormat.Depth32FloatStencil8:
161
+ return context.DEPTH_STENCIL_ATTACHMENT;
162
+
163
+ // --- Everything else is a color attachment ---
164
+ default:
165
+ return context.COLOR_ATTACHMENT0;
166
+ }
167
+ }
168
+
169
+
170
+ /**
171
+ * @param {GLenum} attachment
172
+ */
173
+ export function mapWebGLAttachmentToBufferBit(attachment) {
174
+ switch (attachment) {
175
+ case WebGL2RenderingContext.COLOR_ATTACHMENT0:
176
+ return WebGL2RenderingContext.COLOR_BUFFER_BIT;
177
+ case WebGL2RenderingContext.DEPTH_ATTACHMENT:
178
+ return WebGL2RenderingContext.DEPTH_BUFFER_BIT;
179
+ case WebGL2RenderingContext.STENCIL_ATTACHMENT:
180
+ return WebGL2RenderingContext.STENCIL_BUFFER_BIT;
181
+ case WebGL2RenderingContext.DEPTH_STENCIL_ATTACHMENT:
182
+ return WebGL2RenderingContext.DEPTH_BUFFER_BIT | WebGLRenderingContext.STENCIL_BUFFER_BIT;
183
+ default:
184
+ throw new Error("Unsupported attachment type");
185
+ }
186
+ }
187
+
188
+
189
+ /**
190
+ * Maps a `VertexFormat` variant to WebGL format attributes for use in `vertexAttribPointer` or equivalent functions.
191
+ * @param {VertexFormat} format
192
+ * @returns {WebGLAtttributeParams}
193
+ */
194
+ export function mapVertexFormatToWebGL(format) {
195
+ const gl = WebGL2RenderingContext
196
+ switch (format) {
197
+ // 8-bit int
198
+ case VertexFormat.Uint8:
199
+ return { size: 1, type: gl.UNSIGNED_BYTE, normalized: false };
200
+ case VertexFormat.Uint8x2:
201
+ return { size: 2, type: gl.UNSIGNED_BYTE, normalized: false };
202
+ case VertexFormat.Uint8x3:
203
+ return { size: 3, type: gl.UNSIGNED_BYTE, normalized: false };
204
+ case VertexFormat.Uint8x4:
205
+ return { size: 4, type: gl.UNSIGNED_BYTE, normalized: false };
206
+
207
+ case VertexFormat.Unorm8:
208
+ return { size: 1, type: gl.UNSIGNED_BYTE, normalized: true };
209
+ case VertexFormat.Unorm8x2:
210
+ return { size: 2, type: gl.UNSIGNED_BYTE, normalized: true };
211
+ case VertexFormat.Unorm8x3:
212
+ return { size: 3, type: gl.UNSIGNED_BYTE, normalized: true };
213
+ case VertexFormat.Unorm8x4:
214
+ return { size: 4, type: gl.UNSIGNED_BYTE, normalized: true };
215
+
216
+ case VertexFormat.Snorm8:
217
+ return { size: 1, type: gl.BYTE, normalized: true };
218
+ case VertexFormat.Snorm8x2:
219
+ return { size: 2, type: gl.BYTE, normalized: true };
220
+ case VertexFormat.Snorm8x3:
221
+ return { size: 3, type: gl.BYTE, normalized: true };
222
+ case VertexFormat.Snorm8x4:
223
+ return { size: 4, type: gl.BYTE, normalized: true };
224
+
225
+ case VertexFormat.Sint8:
226
+ return { size: 1, type: gl.BYTE, normalized: false };
227
+ case VertexFormat.Sint8x2:
228
+ return { size: 2, type: gl.BYTE, normalized: false };
229
+ case VertexFormat.Sint8x3:
230
+ return { size: 3, type: gl.BYTE, normalized: false };
231
+ case VertexFormat.Sint8x4:
232
+ return { size: 4, type: gl.BYTE, normalized: false };
233
+
234
+ // 16-bit int
235
+ case VertexFormat.Uint16:
236
+ return { size: 1, type: gl.UNSIGNED_SHORT, normalized: false };
237
+ case VertexFormat.Uint16x2:
238
+ return { size: 2, type: gl.UNSIGNED_SHORT, normalized: false };
239
+ case VertexFormat.Uint16x3:
240
+ return { size: 3, type: gl.UNSIGNED_SHORT, normalized: false };
241
+ case VertexFormat.Uint16x4:
242
+ return { size: 4, type: gl.UNSIGNED_SHORT, normalized: false };
243
+
244
+ case VertexFormat.Unorm16:
245
+ return { size: 1, type: gl.UNSIGNED_SHORT, normalized: true };
246
+ case VertexFormat.Unorm16x2:
247
+ return { size: 2, type: gl.UNSIGNED_SHORT, normalized: true };
248
+ case VertexFormat.Unorm16x3:
249
+ return { size: 3, type: gl.UNSIGNED_SHORT, normalized: true };
250
+ case VertexFormat.Unorm16x4:
251
+ return { size: 4, type: gl.UNSIGNED_SHORT, normalized: true };
252
+
253
+ case VertexFormat.Snorm16:
254
+ return { size: 1, type: gl.SHORT, normalized: true };
255
+ case VertexFormat.Snorm16x2:
256
+ return { size: 2, type: gl.SHORT, normalized: true };
257
+ case VertexFormat.Snorm16x3:
258
+ return { size: 3, type: gl.SHORT, normalized: true };
259
+ case VertexFormat.Snorm16x4:
260
+ return { size: 4, type: gl.SHORT, normalized: true };
261
+
262
+ case VertexFormat.Sint16:
263
+ return { size: 1, type: gl.SHORT, normalized: false };
264
+ case VertexFormat.Sint16x2:
265
+ return { size: 2, type: gl.SHORT, normalized: false };
266
+ case VertexFormat.Sint16x3:
267
+ return { size: 3, type: gl.SHORT, normalized: false };
268
+ case VertexFormat.Sint16x4:
269
+ return { size: 4, type: gl.SHORT, normalized: false };
270
+
271
+ // 32-bit int
272
+ case VertexFormat.Uint32:
273
+ return { size: 1, type: gl.UNSIGNED_INT, normalized: false };
274
+ case VertexFormat.Uint32x2:
275
+ return { size: 2, type: gl.UNSIGNED_INT, normalized: false };
276
+ case VertexFormat.Uint32x3:
277
+ return { size: 3, type: gl.UNSIGNED_INT, normalized: false };
278
+ case VertexFormat.Uint32x4:
279
+ return { size: 4, type: gl.UNSIGNED_INT, normalized: false };
280
+
281
+ case VertexFormat.Sint32:
282
+ return { size: 1, type: gl.INT, normalized: false };
283
+ case VertexFormat.Sint32x2:
284
+ return { size: 2, type: gl.INT, normalized: false };
285
+ case VertexFormat.Sint32x3:
286
+ return { size: 3, type: gl.INT, normalized: false };
287
+ case VertexFormat.Sint32x4:
288
+ return { size: 4, type: gl.INT, normalized: false };
289
+
290
+ // 32-bit floating point
291
+ case VertexFormat.Float32:
292
+ return { size: 1, type: gl.FLOAT, normalized: false };
293
+ case VertexFormat.Float32x2:
294
+ return { size: 2, type: gl.FLOAT, normalized: false };
295
+ case VertexFormat.Float32x3:
296
+ return { size: 3, type: gl.FLOAT, normalized: false };
297
+ case VertexFormat.Float32x4:
298
+ return { size: 4, type: gl.FLOAT, normalized: false };
299
+
300
+ default:
301
+ throw new Error(`Unsupported VertexFormat: ${format}`);
302
+ }
303
+ }
304
+
305
+ /**
306
+ * @param {Uint8Array | Uint16Array | Uint32Array} indices
307
+ */
308
+ export function mapToIndicesType(indices) {
309
+ if (indices instanceof Uint8Array) {
310
+ return GlDataType.UnsignedByte
311
+ }
312
+ if (indices instanceof Uint16Array) {
313
+ return GlDataType.UnsignedShort
314
+ }
315
+ if (indices instanceof Uint32Array) {
316
+ return GlDataType.UnsignedInt
317
+ }
318
+ throw "This is unreachable!"
319
+ }
320
+
321
+ /**
322
+ * @typedef WebGLAtttributeParams
323
+ * @property {number} size
324
+ * @property {GLenum} type
325
+ * @property {boolean} normalized
326
+ */
327
+
328
+ /**
329
+ * Represents a WebGL texture format configuration.
330
+ */
331
+ export class WebGLTextureFormat {
332
+ /**
333
+ * @type {GLenum} Internal format of the texture (e.g., gl.RGBA8)
334
+ */
335
+ internalFormat;
336
+
337
+ /**
338
+ * @type {GLenum} Format of the pixel data (e.g., gl.RGBA)
339
+ */
340
+ format;
341
+
342
+ /**
343
+ * @type {GLenum} Data type of the pixel data (e.g., gl.UNSIGNED_BYTE)
344
+ */
345
+ dataType;
346
+
347
+ /**
348
+ * Creates a WebGLTextureFormat instance.
349
+ * @param {GLenum} internalFormat - The internal format of the texture.
350
+ * @param {GLenum} format - The format of the pixel data.
351
+ * @param {GLenum} dataType - The data type of the pixel data.
352
+ */
353
+ constructor(internalFormat, format, dataType) {
354
+ this.internalFormat = internalFormat;
355
+ this.format = format;
356
+ this.dataType = dataType;
357
+ }
358
+ }
package/src/index.js ADDED
@@ -0,0 +1,15 @@
1
+ export * from './mesh/index.js'
2
+ export * from './math/index.js'
3
+ export * from './material/index.js'
4
+ export * from './constants/index.js'
5
+ export * from './objects/index.js'
6
+ export * from './texture/index.js'
7
+ export * from './shader/index.js'
8
+ export * from './rendertarget/index.js'
9
+ export * from './cameracontrols/index.js'
10
+ export * from './core/index.js'
11
+ export * from './plugins/index.js'
12
+ export * from './renderer/index.js'
13
+ export * from './function.js'
14
+ export * from './loader/index.js'
15
+ export * from './utils/index.js'