Orillusion

    • 注册
    • 登录
    • 搜索
    • 版块
    • 最新
    • 标签

    WGSL | 模块变量和常量的源码案例

    中文社区
    wgsl
    3
    3
    165
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • 支阿怪
      支阿怪 最后由 shuangliu 编辑

      当前WGSL去掉了const声明常量,用var声明变量、let声明常量。
      48c891ec-33c0-46c3-9c71-0d9fc249f252-image.png

      export const Shaders = () => {
          const vertex = `
              struct Output {
                  [[builtin(position)]] Position : vec4<f32>;
                  [[location(0)]] vColor : vec4<f32>;
              };
              [[stage(vertex)]]
              fn main([[builtin(vertex_index)]] VertexIndex: u32) -> Output {
                  var pos = array<vec2<f32>, 3>(
                      vec2<f32>(0.0, 0.5),
                      vec2<f32>(-0.5, -0.5),
                      vec2<f32>(0.5, -0.5)
                  );
          
                  var color = array<vec3<f32>, 3>(
                      vec3<f32>(1.0, 0.0, 0.0),
                      vec3<f32>(0.0, 1.0, 0.0),
                      vec3<f32>(0.0, 0.0, 1.0)
                  );
                  var output: Output;
                  output.Position = vec4<f32>(pos[VertexIndex], 0.0, 1.0);
                  output.vColor = vec4<f32>(color[VertexIndex], 1.0);
                  return output;
              }
          `;
      
          const fragment = `
              [[stage(fragment)]]
              fn main([[location(0)]] vColor: vec4<f32>) -> [[location(0)]] vec4<f32> {
                  return vColor;
              }
          `;
          return {vertex, fragment};
      }
      
      export const Shaders1 = () => {
          const vertex = `
              let pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
                  vec2<f32>(0.0, 0.5),
                  vec2<f32>(-0.5, -0.5),
                  vec2<f32>(0.5, -0.5)
              );
              let color : array<vec3<f32>, 3> = array<vec3<f32>, 3>(
                  vec3<f32>(1.0, 0.0, 0.0),
                  vec3<f32>(0.0, 1.0, 0.0),
                  vec3<f32>(0.0, 0.0, 1.0)
              );
              struct Output {
                  [[builtin(position)]] Position : vec4<f32>;
                  [[location(0)]] vColor : vec4<f32>;
              };
              [[stage(vertex)]]
              fn main([[builtin(vertex_index)]] VertexIndex: u32) -> Output {
                  var output: Output;
                  output.Position = vec4<f32>(pos[VertexIndex], 0.0, 1.0);
                  output.vColor = vec4<f32>(color[VertexIndex], 1.0);
                  return output;
              }
          `;
      
          const fragment = `
              [[stage(fragment)]]
              fn main([[location(0)]] vColor: vec4<f32>) -> [[location(0)]] vec4<f32> {
                  return vColor;
              }
          `;
          return {vertex, fragment};
      }
      
      
      export const ShadersOld = () => {
          const vertex = `
              const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
                  vec2<f32>(0.0, 0.5),
                  vec2<f32>(-0.5, -0.5),
                  vec2<f32>(0.5, -0.5)
              );
              const color : array<vec3<f32>, 3> = array<vec3<f32>, 3>(
                  vec3<f32>(1.0, 0.0, 0.0),
                  vec3<f32>(0.0, 1.0, 0.0),
                  vec3<f32>(0.0, 0.0, 1.0)
              );
              [[builtin(position)]] var<out> Position : vec4<f32>;
              [[builtin(vertex_idx)]] var<in> VertexIndex : i32;
              [[location(0)]] var<out> vColor : vec4<f32>;
              [[stage(vertex)]]
              fn main() -> void {
                  Position = vec4<f32>(pos[VertexIndex], 0.0, 1.0);
                  vColor = vec4<f32>(color[VertexIndex], 1.0);
                  return;
              }
          `;
      
          const fragment = `
              [[location(0)]] var<in> vColor : vec4<f32>;
              [[location(0)]] var<out> fragColor : vec4<f32>;
              [[stage(fragment)]]
              fn main() -> void {
                  fragColor = vColor;
                  return;
              }
          `;
          return {vertex, fragment};
      }
      

      没有捷径,自发光芒

      1 条回复 最后回复 回复 引用 0
      • P
        Perfumer 最后由 编辑

        👽 变量和常量如何声明

        1 条回复 最后回复 回复 引用 0
        • shuangliu
          shuangliu 最后由 shuangliu 编辑

          w3c的doc国内可能不容易打开,可以参看Orillusion的官方翻译
          https://www.orillusion.com/zh/wgsl.html#var-and-let

          1 条回复 最后回复 回复 引用 2
          • First post
            Last post

          Recent Post

          • 目前可以预览demo了

            • 阅读更多
          • A

            这demo太卡了,我机器性能不算差,运行个demo cpu就将近100%

            • 阅读更多
          • A

            没有贴出app.vue的代码

            • 阅读更多
          • @aichangqing 可能是之前版本的cdn缓存没更新,可以清理本地缓存刷新再试一次

            • 阅读更多
          • 36e6af78-b023-4031-9b56-bd8713b44393-image.png

            已经是版本 113.0.5656.0(正式版本)canary (64 位)并且开启chrome://flags/#enable-unsafe-webgpu 为enable,为啥还不能预览demo

            • 阅读更多

          Copyright © 2022 Orillusion | Contact US