Skip to content

Commit ac20aaa

Browse files
author
Mike Solomon
committed
Makes 3d cube
1 parent d94100c commit ac20aaa

File tree

7 files changed

+334
-73
lines changed

7 files changed

+334
-73
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
"purs-tidy": "^0.9.2",
1919
"spago": "^0.20.9",
2020
"vite": "^4.0.4"
21+
},
22+
"dependencies": {
23+
"gl-matrix": "^3.4.3"
2124
}
2225
}

packages.dhall

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,13 @@ let upstream =
102102
https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221229/packages.dhall
103103
sha256:a6af1091425f806ec0da34934bb6c0ab0ac1598620bbcbb60a7d463354e7d87c
104104

105-
in upstream
105+
in upstream
106+
with gl-matrix =
107+
{ dependencies =
108+
["numbers"
109+
]
110+
, repo =
111+
"https://github.com/mikesol/purescript-gl-matrix.git"
112+
, version =
113+
"fixes" -- branch, tag, or commit hash
114+
}

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sandbox-dev.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ let conf = ./spago.dhall
22

33
in conf
44
// { sources = conf.sources # [ "sandbox/**/*.purs" ]
5-
, dependencies = conf.dependencies # [ "aff", "aff-promise", "arraybuffer", "control", "float32", "foldable-traversable", "uint", "web-dom" ]
5+
, dependencies = conf.dependencies # [ "aff", "js-date", "gl-matrix", "numbers", "aff-promise", "arraybuffer", "control", "float32", "foldable-traversable", "uint", "web-dom" ]
66
}

sandbox.dhall

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
let conf = ./spago.dhall
1+
let conf = ./sandbox-dev.dhall
22

33
in conf
4-
// { sources = conf.sources # [ "sandbox/**/*.purs" ]
5-
, dependencies = conf.dependencies # [ "aff", "aff-promise", "arraybuffer", "control", "float32", "foldable-traversable", "uint", "web-dom" ]
6-
, backend = "purs-backend-es build"
4+
// { backend = "purs-backend-es build"
75
}

sandbox/Sandbox.purs

Lines changed: 132 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import Data.UInt (UInt)
2020
import Effect (Effect)
2121
import Effect.Aff (error, launchAff_, throwError)
2222
import Effect.Class (liftEffect)
23+
import GLMatrix.Mat4 as Mat4
24+
import GLMatrix.Vec3 as Vec3
2325
import Unsafe.Coerce (unsafeCoerce)
2426
import Web.DOM.Element (setAttribute)
2527
import Web.DOM.NonElementParentNode (getElementById)
@@ -39,7 +41,7 @@ import Web.GPU.GPUColor (gpuColorDict)
3941
import Web.GPU.GPUColorTargetState (GPUColorTargetState)
4042
import Web.GPU.GPUCommandEncoder (beginRenderPass, finish)
4143
import Web.GPU.GPUCompareFunction as GPUCompareFunction
42-
import Web.GPU.GPUCullMode (none)
44+
import Web.GPU.GPUCullMode as GPUCullMode
4345
import Web.GPU.GPUDepthStencilState (GPUDepthStencilState)
4446
import Web.GPU.GPUDevice (createBuffer, createBindGroupLayout, createBindGroup, createCommandEncoder, createPipelineLayout, createRenderPipeline, createShaderModule, createTexture)
4547
import Web.GPU.GPUDevice as GPUDevice
@@ -61,7 +63,7 @@ import Web.GPU.GPUStoreOp as GPUStoreOp
6163
import Web.GPU.GPUTexture (createView)
6264
import Web.GPU.GPUTextureDescriptor (GPUTextureDescriptor)
6365
import Web.GPU.GPUTextureDimension as GPUTextureDimension
64-
import Web.GPU.GPUTextureFormat (bgra8unorm, depth24plusStencil8)
66+
import Web.GPU.GPUTextureFormat as GPUTextureFormat
6567
import Web.GPU.GPUTextureUsage as GPUTextureUsage
6668
import Web.GPU.GPUVertexAttribute (GPUVertexAttribute)
6769
import Web.GPU.GPUVertexBufferLayout (GPUVertexBufferLayout)
@@ -99,69 +101,133 @@ main :: Effect Unit
99101
main = do
100102
positions :: Float32Array <- fromArray $ hackyFloatConv
101103
[ 1.0
104+
, 1.0
105+
, 1.0
106+
--
107+
, 1.0
108+
, 1.0
102109
, -1.0
103-
, 0.0
110+
--
111+
, 1.0
112+
, -1.0
113+
, 1.0
114+
--
115+
, -1.0
116+
, 1.0
117+
, 1.0
118+
--
119+
, -1.0
120+
, -1.0
121+
, 1.0
122+
--
104123
, -1.0
124+
, 1.0
105125
, -1.0
106-
, 0.0
107-
, 0.0
126+
--
108127
, 1.0
109-
, 0.0
128+
, -1.0
129+
, -1.0
130+
--
131+
, -1.0
132+
, -1.0
133+
, -1.0
110134
]
111135

112136
-- 🎨 Color Vertex Buffer Data
113137
colors :: Float32Array <- fromArray $ hackyFloatConv
114-
[ 1.0
115-
, 0.0
116-
, 0.0
117-
, -- 🔴
118-
0.0
119-
, 1.0
120-
, 0.0
121-
, -- 🟢
122-
0.0
123-
, 0.0
124-
, 1.0
125-
-- 🔵
126-
]
138+
-- [ 0.94, 0.97, 1.0
139+
-- , --
140+
-- 0.82, 0.41, 0.11
141+
-- , --
142+
-- 1.0, 0.75, 0.79
143+
-- , --
144+
-- 0.25, 0.41, 0.88
145+
-- , --
146+
-- 0.98, 0.5, 0.44
147+
-- , --
148+
-- 0.96,0.87, 0.7
149+
-- , --
150+
-- 0.96,0.96,0.96
151+
-- , --
152+
-- 1.0, 0.89, 0.7
153+
-- ]
154+
[0.4,0.5,0.4,1.0,1.0,1.0,0.4,0.5,0.4,1.0,1.0,1.0,0.4,0.5,0.4,1.0,1.0,1.0,0.4,0.5,0.4,1.0,1.0,1.0]
127155
let
128-
makeUniformData yScale = do
129-
uniformData :: Float32Array <- fromArray $ hackyFloatConv
130-
[
131-
-- ♟️ ModelViewProjection Matrix (Identity)
132-
1.0
133-
, 0.0
134-
, 0.0
135-
, 0.0
136-
, 0.0
137-
, 1.0 * yScale
138-
, 0.0
139-
, 0.0
140-
, 0.0
141-
, 0.0
142-
, 1.0
143-
, 0.0
144-
, 0.0
145-
, 0.0
146-
, 0.0
147-
, 1.0
148-
,
149-
-- 🔴 Primary Color
150-
0.9
151-
, 0.1
152-
, 0.3
153-
, 1.0
154-
,
155-
-- 🟣 Accent Color
156-
0.8
157-
, 0.2
158-
, 0.8
159-
, 1.0
160-
]
156+
makeUniformData t = do
157+
-- ♟️ ModelViewProjection Matrix (Identity)
158+
let
159+
-- Mat4.scale Mat4.identity (Vec3.fromValues 1.0 yScale 1.0)
160+
mvp = Mat4.numbers $ flip Mat4.rotateX t $ flip Mat4.rotateZ t $ flip Mat4.scale (Vec3.fromValues 0.25 0.25 0.25) $ Mat4.identity
161+
uniformData :: Float32Array <- fromArray $
162+
hackyFloatConv
163+
mvp <> hackyFloatConv
164+
[
165+
-- 🔴 Primary Color
166+
0.9
167+
, 0.1
168+
, 0.3
169+
, 1.0
170+
,
171+
-- 🟣 Accent Color
172+
0.8
173+
, 0.2
174+
, 0.8
175+
, 1.0
176+
]
161177
pure uniformData
162-
uniformData <- makeUniformData 1.0
178+
uniformData <- makeUniformData 0.0
163179
-- 📇 Index Buffer Data
164-
indices :: Uint16Array <- fromArray $ hackyIntConv [ 0, 1, 2 ]
180+
indices :: Uint16Array <- fromArray $ hackyIntConv
181+
[
182+
--
183+
0
184+
, 1
185+
, 2
186+
----
187+
, 1
188+
, 6
189+
, 2
190+
--
191+
, 2
192+
, 7
193+
, 6
194+
----
195+
, 4
196+
, 7
197+
, 2
198+
--
199+
, 5
200+
, 7
201+
, 4
202+
----
203+
, 5
204+
, 3
205+
, 4
206+
--
207+
, 5
208+
, 3
209+
, 1
210+
----
211+
, 1
212+
, 3
213+
, 0
214+
--
215+
, 5
216+
, 7
217+
, 1
218+
----
219+
, 1
220+
, 7
221+
, 6
222+
--
223+
, 3
224+
, 0
225+
, 4
226+
----
227+
, 4
228+
, 0
229+
, 2
230+
]
165231
-- 🏭 Entry to WebGPU
166232
entry <- window >>= navigator >>= gpu >>= case _ of
167233
Nothing -> do
@@ -289,7 +355,7 @@ fn main(@location(0) inColor: vec3<f32>) -> @location(0) vec4<f32> {
289355
-- 🌑 Depth
290356
let
291357
(depthStencil :: GPUDepthStencilState) = x
292-
{ format: depth24plusStencil8
358+
{ format: GPUTextureFormat.depth24plus -- Stencil8
293359
, depthWriteEnabled: true
294360
, depthCompare: GPUCompareFunction.less
295361
}
@@ -325,7 +391,7 @@ fn main(@location(0) inColor: vec3<f32>) -> @location(0) vec4<f32> {
325391
-- 🌀 Color/Blend State
326392
let
327393
(colorState :: GPUColorTargetState) = x
328-
{ format: bgra8unorm
394+
{ format: GPUTextureFormat.bgra8unorm
329395
}
330396

331397
let
@@ -339,7 +405,7 @@ fn main(@location(0) inColor: vec3<f32>) -> @location(0) vec4<f32> {
339405
let
340406
(primitive :: GPUPrimitiveState) = x
341407
{ frontFace: cw
342-
, cullMode: none
408+
, cullMode: GPUCullMode.none
343409
, topology: triangleList
344410
}
345411

@@ -370,7 +436,7 @@ fn main(@location(0) inColor: vec3<f32>) -> @location(0) vec4<f32> {
370436
let
371437
(config :: GPUCanvasConfiguration) = x
372438
{ device
373-
, format: bgra8unorm
439+
, format: GPUTextureFormat.bgra8unorm
374440
, usage:
375441
GPUTextureUsage.renderAttachment .|.
376442
GPUTextureUsage.copySrc
@@ -380,9 +446,9 @@ fn main(@location(0) inColor: vec3<f32>) -> @location(0) vec4<f32> {
380446
let
381447
(depthTextureDesc :: GPUTextureDescriptor) = x
382448
{ size: gpuExtent3DWHD canvasWidth canvasHeight 1
383-
, format: depth24plusStencil8
384-
, usage: GPUTextureUsage.renderAttachment .|. GPUTextureUsage.copySrc
385-
, dimension: GPUTextureDimension.twoD
449+
, format: GPUTextureFormat.depth24plus
450+
, usage: GPUTextureUsage.renderAttachment -- .|. GPUTextureUsage.copySrc
451+
--, dimension: GPUTextureDimension.twoD
386452
}
387453
depthTexture <- liftEffect $ createTexture device depthTextureDesc
388454
depthTextureView <- liftEffect $ createView depthTexture
@@ -402,9 +468,9 @@ fn main(@location(0) inColor: vec3<f32>) -> @location(0) vec4<f32> {
402468
, depthClearValue: 1.0
403469
, depthLoadOp: GPULoadOp.clear
404470
, depthStoreOp: GPUStoreOp.store
405-
, stencilClearValue: 0
406-
, stencilLoadOp: GPULoadOp.clear
407-
, stencilStoreOp: GPUStoreOp.store
471+
-- , stencilClearValue: 0
472+
--, stencilLoadOp: GPULoadOp.clear
473+
--, stencilStoreOp: GPUStoreOp.store
408474
}
409475

410476
let
@@ -436,11 +502,10 @@ fn main(@location(0) inColor: vec3<f32>) -> @location(0) vec4<f32> {
436502
setVertexBuffer passEncoder 1 colorBuffer
437503
setIndexBuffer passEncoder indexBuffer uint16
438504
tn <- getTime <$> now
439-
let y = sin (tn * 0.001 * pi) * 0.4 + 0.6
440-
newBuffer <- makeUniformData y
505+
newBuffer <- makeUniformData (tn * 0.001)
441506
writeBuffer queue uniformBuffer 0 (fromFloat32Array newBuffer)
442507
setBindGroup passEncoder 0 uniformBindGroup
443-
drawIndexedWithInstanceCount passEncoder 3 1
508+
drawIndexedWithInstanceCount passEncoder 36 1
444509
end passEncoder
445510
toSubmit <- finish commandEncoder
446511
submit queue [ toSubmit ]

0 commit comments

Comments
 (0)