3d,正交投影,所有面都一种颜色

这篇具有很好参考价值的文章主要介绍了3d,正交投影,所有面都一种颜色。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>てst</title>
</head>

<body>
  <!-- canvas:用来展示WebGPU渲染的结果 -->
  <canvas id="webgpu" width="500" height="500"></canvas>
  translationx:<input type="range" min="0" max="500" step="0.1" name="translationx" value="230.2" /><br>
  translationy:<input type="range" min="0" max="500" step="0.1" name="translationy" value="230.2" /><br>
  rotationx:<input type="range" min="0" max="360" step="0.1" name="rotationx" value="0" /><br>
  rotationy:<input type="range" min="0" max="360" step="0.1" name="rotationy" value="0" /><br>
  rotationz:<input type="range" min="0" max="360" step="0.1" name="rotationz" value="0" /><br>
  scalex:<input type="range" min="-5" max="5" step="0.1" name="scalex" value="1.1" /><br>
  scaley:<input type="range" min="-5" max="5" step="0.1" name="scaley" value="1.1" /><br>

  <script type="module">
    //"C:\Program Files\Google\Chrome\Application\chrome.exe" --disable-web-security --user-data-dir="D:\angular\webgpu\n" --disable-site-isolation-trials

    import * as glMatrix from './dist/esm/index.js';

    function createTranVertices() {
  const vertexData = new Float32Array([
    // 左栏
    0, 0, 0,
    30, 0, 0,
    0, 150, 0,
    30, 150, 0,
 
    // 顶部梯级
    30, 0, 0,
    100, 0, 0,
    30, 30, 0,
    100, 30, 0,
 
    // 中间梯级
    30, 60, 0,
    70, 60, 0,
    30, 90, 0,
    70, 90, 0,
 
    // 左栏后退
    0, 0, 30,
    30, 0, 30,
    0, 150, 30,
    30, 150, 30,
 
    // 顶部回档
    30, 0, 30,
    100, 0, 30,
    30, 30, 30,
    100, 30, 30,
 
    // 中间梯级后面
    30, 60, 30,
    70, 60, 30,
    30, 90, 30,
    70, 90, 30,
  ]);
 
  const indexData = new Uint32Array([
    // 正面
    0,  1,  2,    2,  1,  3,  // 左栏
    4,  5,  6,    6,  5,  7,  // 顶部运行
    8,  9, 10,   10,  9, 11,  // 中间运行
 
    // 后退
    12,  13,  14,   14, 13, 15,  // 左栏后退
    16,  17,  18,   18, 17, 19,  // 顶部跑回
    20,  21,  22,   22, 21, 23,  // 中间跑回
 
    0, 5, 12,   12, 5, 17,   // 顶部
    5, 7, 17,   17, 7, 19,   // 右上横档
    6, 7, 18,   18, 7, 19,   // 顶部梯级底部
    6, 8, 18,   18, 8, 20,   // 顶部和中间梯级之间
    8, 9, 20,   20, 9, 21,   // 中间梯级顶部
    9, 11, 21,  21, 11, 23,  // 右中横档
    10, 11, 22, 22, 11, 23,  // 中间梯级底部
    10, 3, 22,  22, 3, 15,   // 右干
    2, 3, 14,   14, 3, 15,   // 底部
    0, 2, 12,   12, 2, 14,   // 左边
  ]);
 
  return {
    vertexData,
    indexData,
    numVertices: indexData.length,
  };
    }

    async function main() {
      const adapter = await navigator.gpu?.requestAdapter();
      const device = await adapter?.requestDevice();
      if (!device) {
        fail('need a browser that supports WebGPU');
        return;
      }

      // Get a WebGPU context from the canvas and configure it
      const canvas = document.querySelector('canvas');
      const context = canvas.getContext('webgpu');
      const presentationFormat = navigator.gpu.getPreferredCanvasFormat();

      context.configure({
        device,
        format: presentationFormat,
        alphaMode: 'premultiplied',
      });

      const module = device.createShaderModule({
        code: `
       struct Uniforms {
          color: vec4f,
          matrix: mat4x4f,
        };

        struct Vertex {
          @location(0) position: vec4f,
        };

        struct VSOutput {
          @builtin(position) position: vec4f,
        };

        @group(0) @binding(0) var<uniform> uni: Uniforms;

        @vertex fn vs(vert: Vertex) -> VSOutput {
          var vsOut: VSOutput;

           vsOut.position = uni.matrix * vert.position;
  return vsOut;
        }

        @fragment fn fs(vsOut: VSOutput) -> @location(0) vec4f {
          return uni.color;
        }
    `
      });

      const pipeline = device.createRenderPipeline({
        label: 'just 2d position',
        layout: 'auto',
        vertex: {
          module,
          entryPoint: 'vs',
          buffers: [
            {
              arrayStride: (3) * 4, // (2) floats, 4 bytes each
              attributes: [
                { shaderLocation: 0, offset: 0, format: 'float32x3' },  // position
              ],
            },
          ],
        },
        fragment: {
          module,
          entryPoint: 'fs',
          targets: [{ format: presentationFormat }],
        },
      });

      // color, resolution, padding, matrix
      const uniformBufferSize = (4  + 16) * 4;
      const uniformBuffer = device.createBuffer({
        label: 'uniforms',
        size: uniformBufferSize,
        usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
      });

      const uniformValues = new Float32Array(uniformBufferSize / 4);

      // offsets to the various uniform values in float32 indices
      const kColorOffset = 0;
       
      const kMatrixOffset = 4;

      const colorValue = uniformValues.subarray(kColorOffset, kColorOffset + 4);
      const matrixValue = uniformValues.subarray(kMatrixOffset, kMatrixOffset + 16);

      // The color will not change so let's set it once at init time
      colorValue.set([Math.random(), Math.random(), Math.random(), 1]);

      
      const { vertexData, indexData, numVertices } = createTranVertices();
      const vertexBuffer = device.createBuffer({
        label: 'vertex buffer vertices',
        size: vertexData.byteLength,
        usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
      });
      device.queue.writeBuffer(vertexBuffer, 0, vertexData);

const indexBuffer = device.createBuffer({
    label: 'index buffer',
    size: indexData.byteLength,
    usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
  });
  device.queue.writeBuffer(indexBuffer, 0, indexData);
  
      const bindGroup = device.createBindGroup({
        label: 'bind group for object',
        layout: pipeline.getBindGroupLayout(0),
        entries: [
          { binding: 0, resource: { buffer: uniformBuffer } },
        ],
      });

      const renderPassDescriptor = {
        label: 'our basic canvas renderPass',
        colorAttachments: [
          {
            // view: <- to be filled out when we render
            loadOp: 'clear',
            storeOp: 'store',
          },
        ],
      };

      function render() {

        // Get the current texture from the canvas context and
        // set it as the texture to render to.
        renderPassDescriptor.colorAttachments[0].view =
          context.getCurrentTexture().createView();

        const encoder = device.createCommandEncoder();
        const pass = encoder.beginRenderPass(renderPassDescriptor);
        pass.setPipeline(pipeline);
        pass.setVertexBuffer(0, vertexBuffer);
pass.setIndexBuffer(indexBuffer, 'uint32');
        let translationx = document
          .querySelector('input[name="translationx"]')
          .value;
        let translationy = document
          .querySelector('input[name="translationy"]')
          .value;
        let rotationx = document
          .querySelector('input[name="rotationx"]')
          .value;
        rotationx = rotationx * Math.PI / 180;
        
        let rotationy = document
          .querySelector('input[name="rotationy"]')
          .value;
        rotationy = rotationy * Math.PI / 180;
        
        let rotationz = document
          .querySelector('input[name="rotationz"]')
          .value;
        rotationz = rotationz * Math.PI / 180;

        let scalex = document
          .querySelector('input[name="scalex"]')
          .value;
        let scaley = document
          .querySelector('input[name="scaley"]')
          .value;

        let translationMatrix = glMatrix.mat4.create();
        glMatrix.mat4.translate(translationMatrix, translationMatrix, glMatrix.vec3    .fromValues(translationx, translationy,1));

        let rotationMatrix = glMatrix.mat4.create();
       // glMatrix.mat4.rotate(rotationMatrix, rotationMatrix, 1, glMatrix.vec3.fromValues(Math.sin(rotation), Math.cos(rotation), 0));
        //glMatrix.mat4.rotate(rotationMatrix, rotationMatrix, rotation);

        glMatrix.mat4.rotateX(rotationMatrix, rotationMatrix, rotationx);
        glMatrix.mat4.rotateY(rotationMatrix, rotationMatrix, rotationy);
        glMatrix.mat4.rotateZ(rotationMatrix, rotationMatrix, rotationz);

        let scaleMatrix = glMatrix.mat4.create();
        glMatrix.mat4.scale(scaleMatrix, scaleMatrix, [scalex, scaley, 1]);

        // make a matrix that will move the origin of the 'F' to its center.
        // const moveOriginMatrix = mat3.translation([-50, -75]);
        let moveOriginMatrix = glMatrix.mat4.create();
        //glMatrix.mat4.translate(moveOriginMatrix, moveOriginMatrix, glMatrix.vec2.fromValues(-50, -70));

//-------------
 //固定相机,正交投影
 //  const aspect = Math.abs(canvas.width / canvas.height);
  let projectionMatrix = glMatrix.mat4.create();
 // glMatrix.mat4.perspective(projectionMatrix, (2 * Math.PI) / 5, aspect, 1, 100.0);
  // glMatrix.mat4.projection(projectionMatrix,400,canvas.clientWidth, canvas.clientHeight);
   glMatrix.mat4.ortho(projectionMatrix,0,canvas.clientWidth, 0,canvas.clientHeight,400,-400);
   
   /*
   mat4.ortho(
        0,                   // 左边
        canvas.clientWidth,  // 正确的
        canvas.clientHeight, // 底部
        0,                   // 顶部
        400,                 // 靠近
        -400,                // 远的
        matrixValue,         // 夏令时
    ); 
    */
//-------------
        let matrix = glMatrix.mat4.create();
        //glMatrix.mat4.multiply(matrix, translationMatrix, rotationMatrix);
        glMatrix.mat4.multiply(matrix,projectionMatrix, translationMatrix);
        glMatrix.mat4.multiply(matrix, matrix, rotationMatrix);
        glMatrix.mat4.multiply(matrix, matrix, scaleMatrix);

        // matrix = mat3.multiply(matrix, moveOriginMatrix);
        glMatrix.mat4.multiply(matrix, matrix, moveOriginMatrix);

/*
        console.log('matrix:' + matrix);
        console.log('matrix0-3:' + matrix.slice(0, 3));
        console.log('matrixValue:' + matrixValue);
*/
        // Set the uniform values in our JavaScript side Float32Array
       // resolutionValue.set([canvas.width, canvas.height]);
        
     /*
        matrixValue.set([
          ...matrix.slice(0, 3), 0,
          ...matrix.slice(3, 6), 0,
          ...matrix.slice(6, 9), 0,
        ]);
*/
 //console.log(matrix) 
  matrixValue.set(matrix);

        // upload the uniform values to the uniform buffer
        device.queue.writeBuffer(uniformBuffer, 0, uniformValues);

        pass.setBindGroup(0, bindGroup);
        // pass.drawIndexed(numVertices);
        //pass.draw(3);
        pass.drawIndexed(numVertices);
        pass.end();

        const commandBuffer = encoder.finish();
        device.queue.submit([commandBuffer]);
      }

      //-----------------
      document
        .querySelector('input[name="translationx"]')
        .addEventListener("input", (e) => {
          render();
        });

      document
        .querySelector('input[name="translationy"]')
        .addEventListener("input", (e) => {
          render();
        });
      document
        .querySelector('input[name="rotationx"]')
        .addEventListener("input", (e) => {
          render();
        });
        document
        .querySelector('input[name="rotationy"]')
        .addEventListener("input", (e) => {
          render();
        });
        document
        .querySelector('input[name="rotationz"]')
        .addEventListener("input", (e) => {
          render();
        });
      document
        .querySelector('input[name="scalex"]')
        .addEventListener("input", (e) => {
          render();
        });
      document
        .querySelector('input[name="scaley"]')
        .addEventListener("input", (e) => {
          render();
        });

      const observer = new ResizeObserver(entries => {
        for (const entry of entries) {
          const canvas = entry.target;
          const width = entry.contentBoxSize[0].inlineSize;
          const height = entry.contentBoxSize[0].blockSize;
          canvas.width = Math.max(1, Math.min(width, device.limits.maxTextureDimension2D));
          canvas.height = Math.max(1, Math.min(height, device.limits.maxTextureDimension2D));
          // re-render
          render();
        }
      });
      observer.observe(canvas);
    }

    function fail(msg) {
      alert(msg);
    }

    main();
  </script>
</body>

</html>文章来源地址https://www.toymoban.com/news/detail-837303.html

到了这里,关于3d,正交投影,所有面都一种颜色的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 计算机图形学-正交投影与透视投影

    本专栏内容整理了GAMES101的计算机图形学课程的主要内容,作为我学习计算机图形学的一份复习备份或叫做笔记。内容中如有错误,或有其他建议,欢迎大家指出。 附上GAMES101计算机图形学课程: GAMES101: 现代计算机图形学入门正在上传…重新上传取消https://sites.cs.ucsb.edu/~li

    2023年04月08日
    浏览(25)
  • 第五章 OpenGL ES 基础-透视投影矩阵与正交投影矩阵

    第一章 OpenGL ES 基础-屏幕、纹理、顶点坐标 第二章 OpenGL ES 基础-GLSL语法简单总结 第三章 OpenGL ES 基础-GLSL渲染纹理 第四章 OpenGL ES 基础-位移、缩放、旋转原理 第五章 OpenGL ES 基础-透视投影矩阵与正交投影矩阵 模型都是3D的,但屏幕是2D的。如何将3D空间投影到2D平面,还能保

    2024年03月09日
    浏览(31)
  • Android OpenGL ES 学习(四) -- 正交投影

    OpenGL 学习教程 Android OpenGL ES 学习(一) – 基本概念 Android OpenGL ES 学习(二) – 图形渲染管线和GLSL Android OpenGL ES 学习(三) – 绘制平面图形 Android OpenGL ES 学习(四) – 正交投屏 Android OpenGL ES 学习(五) – 渐变色 Android OpenGL ES 学习(六) – 使用 VBO、VAO 和 EBO/IBO 优化程序 Android OpenG

    2024年02月13日
    浏览(30)
  • 正交投影的矩阵(基变换与过渡矩阵的例子)

    在 n n n 维欧几里得空间 V V V 中,有子空间 W W W . 如果用自然基 ( e i ) 1 ≤ i ≤ n (mathbf{e}_i)_{1leq i leq n} ( e i ​ ) 1 ≤ i ≤ n ​ ,设 W = s p a n ( w 1 , … , w d )    ( 0 d n ) W=mathrm{span}(w_1, ldots, w_d); (0 d n) W = span ( w 1 ​ , … , w d ​ ) ( 0 d n ) . 将这个基作Schmidt正交化(并单位化

    2024年04月13日
    浏览(29)
  • 三维变换矩阵实战——三维点云的旋转、缩放、镜像、错切、平移、正交投影

    旋转矩阵:右边矩阵是点云的原始坐标,左边的是旋转矩阵     可视化:绕x轴旋转90度 代码: 旋转矩阵:    可视化:绕y轴旋转180度 代码: 旋转矩阵:    可视化:绕z轴旋转90度 代码: 旋转矩阵:  线绕哪个轴转,xyz矩阵就和哪和轴的旋转矩阵先计算      可视化:先

    2024年02月04日
    浏览(69)
  • 【Unity3D】正交视图与透视视图 ( 正交视图概念 | 透视视图概念 | 观察点 | 正交视图作用 | 摄像机广角设定 | 透视畸变效果 )

    透视视图 ( Perspective View ) : 近大远小 , 符合正常人眼观察 3D 世界的规律 ; 近大 : 物体 距离 观察点 ( 视点 ) 比较近时 , 显示效果比较大 ; 远小 : 物体 距离 观察点 ( 视点 ) 比较远时 , 显示效果比较小 ; 下图就是利用了 透视视图 原理 , 照像机离鸟很近 , 离人很远 ; 在 Unity 编辑

    2024年01月16日
    浏览(37)
  • 裸眼3D全息投影技术

    现在这个智能时代,聊天的时候不说点黑科技,都不好意思和人开口。今天,小画就要和大家聊聊投影领域的黑科技——全息投影。 在看好莱坞大片的时候,有一个场景我们非常熟悉:主角挥一下手,眼前就会出现一块立体的虚拟的显示屏,屏幕上的内容主角可以任意切换—

    2023年04月19日
    浏览(31)
  • OpenCV颜色识别(所有颜色均可识别)

    欢迎访问我的博客sakuraの绘梨衣 本文中的颜色识别为红色,颜色阈值设置如下: 识别其他颜色可以参考HSV颜色阈值设置进行更改 下面是识别代码,注释很详细: 下面是识别效果:

    2024年02月12日
    浏览(27)
  • 伪3d原理解释 主要是透视投影

    当我们将图像投影到一个旋转的表面上时,我们需要考虑以下几个方面: 像素位置的计算:对于每个显示窗口中的像素,我们需要计算它在旋转表面上的位置。在代码中,使用了以下公式来计算旋转表面上的位置: px = x / z * sc py = y / z * sc 这里,x和y表示像素在显示窗口中的

    2024年02月16日
    浏览(32)
  • Origin(Pro):3D图-投影、垂线、标签

    以XYZ形式的数据为例,Country列为标签。 插入3D Scatter图,首先设置散点的颜色、大小等格式。 三维图的投影(Projection)、垂线(Drop lines)和标签(Label)均在Plot Details对话框下设置。 1、投影(Projection):在Plot Details对话框下,勾选Original下面的Projiection选项,即可在坐标屏

    2024年02月12日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包