本文介绍了着色器可以旋转形状以面对相机吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个场景,其中球出现在3D空间中.三角球花费很多资源.因此,我使用具有球纹理的2d曲面(四边形)进行了此操作.但是,现在每次相机移动时,我都需要调整形状的方向.我使用位置转换和LookAt方法进行此操作.问题是我可以优化它吗?如果可以使用着色器旋转形状,则将大有帮助.

I made a scene where balls appear in 3D space. Triangle balls spend a lot of resources. So I did this using 2d surfaces (quad) with a ball texture. But now I need to adjust the direction of the shape every time the camera moves. I do this using position transformation and the LookAt method. The question is can I optimize this? If it is possible to rotate the shapes with shader, this will greatly help.

using UnityEngine;

public class WorldSurf : MonoBehaviour
{
    GameObject[] matrix;

    int xSize =  20;
    int ySize =  20;
    int zSize =  20;

    // Start is called before the first frame update
    void Start()
    {
        matrix = new GameObject[xSize * ySize * zSize];

        //var shader = Shader.Find("Legacy Shaders/Diffuse");
        var shader = Shader.Find("Sprites/Default");
        //var texture = Resources.Load<Texture>("Textures/Ball_01");

        var i = 0;

        for (var x = 0f; x < xSize; ++x)
        {
            for (var y = 0f; y < ySize; ++y)
            {
                for (var z = 0f; z < zSize; ++z)
                {
                    var texture = Resources.Load<Texture>("Textures/Ball_" + ((int)Random.Range(0, 15)).ToString("00"));

                    matrix[i++] = CreateQuad(x * 3, y * 3, z * 3, shader, texture);
                }
            }
        }
    }

    static GameObject CreateQuad(float x, float y, float z, Shader shader, Texture texture)
    {
        var quad = GameObject.CreatePrimitive(PrimitiveType.Quad);

        quad.transform.position = new Vector3(x, y, z);
        quad.transform.forward = Camera.main.transform.forward;

        var rend = quad.GetComponent<Renderer>();

        rend.material.shader      = shader;
        rend.material.mainTexture = texture;
        //rend.material.color = Color.red;

        return quad;
    }

    // Update is called once per frame
    void Update()
    {
        var pos = Camera.main.transform.position;

        foreach (var itm in matrix)
        {
            itm.transform.LookAt(pos);
        }
    }
}

推荐答案

通常是的,在这种特定情况下,您希望四边形与相机对齐将非常容易.

Generally yes, and in this specific case where you want a quad to align with the camera makes doing so very easy.

您想要的被称为广告牌着色器".这是来自 Wikibooks

What you want is called a "billboard shader". Here is an example from Wikibooks:

Shader "Cg  shader for billboards" {
   Properties {
      _MainTex ("Texture Image", 2D) = "white" {}
      _ScaleX ("Scale X", Float) = 1.0
      _ScaleY ("Scale Y", Float) = 1.0
   }
   SubShader {
      Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
      ZWrite Off
      Blend SrcAlpha OneMinusSrcAlpha

      Pass {
         CGPROGRAM

         #pragma vertex vert
         #pragma fragment frag

         // User-specified uniforms
         uniform sampler2D _MainTex;
         uniform float _ScaleX;
         uniform float _ScaleY;

         struct vertexInput {
            float4 vertex : POSITION;
            float4 tex : TEXCOORD0;
         };
         struct vertexOutput {
            float4 pos : SV_POSITION;
            float4 tex : TEXCOORD0;
         };

         vertexOutput vert(vertexInput input)
         {
            vertexOutput output;

            output.pos = mul(UNITY_MATRIX_P,
              mul(UNITY_MATRIX_MV, float4(0.0, 0.0, 0.0, 1.0))
              + float4(input.vertex.x, input.vertex.y, 0.0, 0.0)
              * float4(_ScaleX, _ScaleY, 1.0, 1.0));

            output.tex = input.tex;

            return output;
         }

         float4 frag(vertexOutput input) : COLOR
         {
            return tex2D(_MainTex, float2(input.tex.xy));
         }

         ENDCG
      }
   }
}

并对此进行了解释:

这将产生如下输出:

这篇关于着色器可以旋转形状以面对相机吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:23