【Unity3D】陰陽師畫符

前言

眾所周知,2016年對“陰陽師”而言是不一樣的一年。作為網(wǎng)易在第三季度推出的新游,創(chuàng)新地將原汁原味的和風(fēng)美學(xué)體驗帶給用戶,獲得了超出預(yù)期的市場反應(yīng)。9月2日首發(fā)當(dāng)天即獲App Store編輯選薦,更在12月發(fā)布的App Annie中國區(qū)2016年度十佳游戲中占據(jù)一席之地。10月初,其中文版在全球多個國家的蘋果應(yīng)用商店上架,根據(jù)11月11日首部資料片上線時的App Annie數(shù)據(jù),《陰陽師》獲得極具突破的矚目和成績:中國免費(fèi)榜第1、暢銷榜第1,加拿大暢銷榜第1,澳大利亞暢銷榜第1,新西蘭暢銷榜第1,英國暢銷榜第8,美國暢銷榜第10等等傲人的成績。

2016年度十佳游戲

作為其中的核心玩法-畫符,是其受到眾多玩家青睞的重大原因。
本篇將告訴你怎么在Unity3D中怎么實現(xiàn)這一效果。

思路?

如何實現(xiàn)在屏幕上畫東西?

  1. Unity內(nèi)置的LineRenderer
  2. Shader
  • 這里我們選擇用OpenGL來實現(xiàn)。
    官方幫助文檔里面,我們可以找到GL這個API。

代碼實現(xiàn)

using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    public void GenerateText()
    {
        Texture2D tmpTex = new Texture2D(300,400);

        for (int i = 1; i < allPoints.Count; i++)
        {
            Vector2 tmpFront = allPoints[i - 1];
            Vector2 tmpBack = allPoints[i];

            for (int j = 1; j < 100; j++)
            {
                int xx = (int)(Mathf.Lerp(tmpFront.x * tmpTex.width, tmpBack.x*tmpTex.width, j / 100f));
                int yy = (int)(Mathf.Lerp(tmpFront.y * tmpTex.height, tmpBack.y * tmpTex.height, j / 100f));
                tmpTex.SetPixel(xx, yy, Color.yellow);
            }
        }

        tmpTex.Apply();

        GetComponent<Renderer>().material.mainTexture = tmpTex;

    }


    static Material lineMaterial;
    static void CreateLineMaterial()
    {
        if (!lineMaterial)
        {
            // Unity has a built-in shader that is useful for drawing
            // simple colored things.
            Shader shader = Shader.Find("Hidden/Internal-Colored");
            lineMaterial = new Material(shader);
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            // Turn on alpha blending
            lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            // Turn backface culling off
            lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
            // Turn off depth writes
            lineMaterial.SetInt("_ZWrite", 0);
        }
    }

    // Will be called after all regular rendering is done
    public void OnRenderObject()
    {
        CreateLineMaterial();
        // Apply the line material
        lineMaterial.SetPass(0);

        GL.PushMatrix();
        // Set transformation matrix for drawing to
        // match our transform
        GL.MultMatrix(transform.localToWorldMatrix);

        // Draw lines
        GL.Begin(GL.LINES);

        // 將透視投影變成正交投影
        GL.LoadOrtho();

        GL.Color(Color.yellow);

        for (int i = 1; i < allPoints.Count; i++)
        {
            Vector2 tmpFront = allPoints[i-1];
            Vector2 tmpBack = allPoints[i];

            GL.Vertex3(tmpFront.x, tmpFront.y, 0);
            GL.Vertex3(tmpBack.x, tmpBack.y, 0);
        }

        GL.End();
        GL.PopMatrix();
    }

    List<Vector2> allPoints = new List<Vector2>();

    private void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Vector2 tmpPos = Camera.main.ScreenToViewportPoint(Input.mousePosition);

            allPoints.Add(tmpPos);
        }

        if (Input.GetMouseButtonUp(0))
        {
            GenerateText();
            //清空屏幕上的點(diǎn)
            allPoints.Clear();
        }
    }
}

效果

畫符.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.產(chǎn)品優(yōu)化概述 1.1影響ASO主要因素 1.應(yīng)用名稱; 2.應(yīng)用的關(guān)鍵字或者標(biāo)簽; 3.應(yīng)用的描述; 4.應(yīng)用...
    其名為鯤北冥有魚閱讀 5,648評論 1 9
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,716評論 25 709
  • 2017年憑借自己的力量,終于弄好小窩,妥妥的安置了肉身,實現(xiàn)了有尊嚴(yán)的住和有規(guī)律的吃。?,F(xiàn)在,我的精神卻無...
    幾點(diǎn)周閱讀 138評論 0 0
  • 再次踏上這片熟悉的土地,回到家,窗外的一切都已不復(fù)存在,你,還記得我嗎?我,從未忘記! 還記得那個炎熱的夏天,太陽...
    像風(fēng)如云閱讀 529評論 0 1
  • 今天逛慶山的微博,看到一句話:“每個人都有自己的故事。每個人都很珍貴?!?忽然覺得很感動。 慶山叫做“安妮寶貝”的...
    安桐2016閱讀 453評論 2 2

友情鏈接更多精彩內(nèi)容