• 版块
  • 最新
  • 标签
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(不使用皮肤)
  • 不使用皮肤
折叠

Orillusion

0

在线

524

用户

151

主题

408

帖子

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

已定时 已固定 已锁定 已移动 中文社区
wgsl
3 帖子 3 发布者 195 浏览
    • 从旧到新
    • 从新到旧
    • 最多赞同
回复
  • 在新帖中回复
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • 支阿怪支 离线
    支阿怪支 离线
    支阿怪
    写于 最后由 shuangliu 编辑
    #1

    当前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 离线
    P 离线
    Perfumer
    写于 最后由 编辑
    #2

    👽 变量和常量如何声明

    1 条回复 最后回复
    0
  • shuangliuS 离线
    shuangliuS 离线
    shuangliu
    写于 最后由 shuangliu 编辑
    #3

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

    1 条回复 最后回复
    2

Copyright © 2023 Orillusion | Contact US

  • 登录

  • 没有帐号? 注册

  • 登录或注册以进行搜索。
  • 第一个帖子
    最后一个帖子
0
  • 版块
  • 最新
  • 标签
  • 登录

  • 没有帐号? 注册

  • 登录或注册以进行搜索。