art-cubemap贴图制作

本文介绍如何在Unity中创建和使用Cubemap。包括从素材制作Cubemap、利用Unity工具将六张图片合成Cubemap以及将Cubemap拆分为六张图片的方法。此外还介绍了如何通过脚本将场景渲染为Cubemap。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


title: art-cubemap贴图制作
categories: Art
tags: [art, cubemap, 贴图, 制作, ta]
date: 2019-04-02 10:37:47
comments: false

art-cubemap贴图制作


前篇

  • cubemap 素材下载的站点 - http://www.humus.name/index.php?page=Textures&start=0
  • CubeMapGen : cubemap 制作工具 ( 没研究怎么玩, 先 Mark ) - https://code.google.com/archive/p/cubemapgen/downloads
  • How To Generate The Environment Cubemap In Unity ( 渲染unity中的场景到一个cubemap中 ) - https://lmhpoly.com/how-to-generate-the-environment-cubemap-in-unity/

unity 将 六张图 合成为 cubemap

素材来源是六张图

  • 原素材展开图

    原素材命名

  • unity中制作cubemap. 创建 cubemap : assets -> create -> legacy -> cubemap

    因为坐标系的不同, 所以要将原素材命名为 negx 放在unity中的 +x 位置, posx 放在 -x 位置


unity 将 cubemap 分割成 六张图

参考: Cube Map Seperator Here! - https://forum.unity.com/threads/cube-map-seperator-here.239101/

  1. 编辑器扩展代码

    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEditor;
    using UnityEngine;
    
    public class CubeSplitter : EditorWindow {
    	Cubemap splitCube;
    	Color[] CubeMapColors;
    	int splitSize;
    
    	[MenuItem ("Tools/CubeSplitter", false, 20001)]
    	public static void OpenCubeSplitter () {
    		var win = GetWindow<CubeSplitter> ("CubeSplitter");
    		win.Show ();
    	}
    
    	void OnGUI () {
    		GUILayout.Label ("Choose the Cube Map you want to save as 6 images and click EXPORT!", EditorStyles.boldLabel);
    		splitCube = EditorGUILayout.ObjectField ("My Cubemap:", splitCube, typeof (Cubemap), false) as Cubemap;
    		GUILayout.Label ("Make sure to set the Size to the same as the Cubemap you are using", EditorStyles.boldLabel);
    		splitSize = EditorGUILayout.IntField ("CubeMap Size: ", splitSize);
    
    		if (GUILayout.Button ("EXPORT!")) {
    			if (splitCube) {
    				Export ();
    			} else {
    				Debug.Log ("Forget Something?");
    			}
    		}
    	}
    
    	void Export () {
    		var filePath = AssetDatabase.GetAssetPath (splitCube);
    		Texture2D tex = new Texture2D (splitSize, splitSize, TextureFormat.RGB24, false);
    
    		CubeMapColors = splitCube.GetPixels (CubemapFace.PositiveY);
    		tex.SetPixels (CubeMapColors, 0);
    		tex.Apply ();
    		byte[] bytes = tex.EncodeToPNG ();
    		File.WriteAllBytes (filePath + "_Bot.png", bytes);
    
    		CubeMapColors = splitCube.GetPixels (CubemapFace.NegativeY);
    		tex.SetPixels (CubeMapColors, 0);
    		tex.Apply ();
    		bytes = tex.EncodeToPNG ();
    		File.WriteAllBytes (filePath + "_Top.png", bytes);
    
    		CubeMapColors = splitCube.GetPixels (CubemapFace.PositiveX);
    		tex.SetPixels (CubeMapColors, 0);
    		tex.Apply ();
    		bytes = tex.EncodeToPNG ();
    		File.WriteAllBytes (filePath + "_Left.png", bytes);
    
    		CubeMapColors = splitCube.GetPixels (CubemapFace.NegativeX);
    		tex.SetPixels (CubeMapColors, 0);
    		tex.Apply ();
    		bytes = tex.EncodeToPNG ();
    		File.WriteAllBytes (filePath + "_Right.png", bytes);
    
    		CubeMapColors = splitCube.GetPixels (CubemapFace.PositiveZ);
    		tex.SetPixels (CubeMapColors, 0);
    		tex.Apply ();
    		bytes = tex.EncodeToPNG ();
    		File.WriteAllBytes (filePath + "_Front.png", bytes);
    
    		CubeMapColors = splitCube.GetPixels (CubemapFace.NegativeZ);
    		tex.SetPixels (CubeMapColors, 0);
    		tex.Apply ();
    		bytes = tex.EncodeToPNG ();
    		File.WriteAllBytes (filePath + "_Back.png", bytes);
    
    		this.Close ();
    	}
    }
    
  2. 拖入要分割的 cubemap, 设置分辨率为 cubemap 的分辨率. 点击 EXPORT. done!


unity 渲染场景 cubemap

把当前场景渲染成一个 cubemap

参考: How To Generate The Environment Cubemap In Unity - https://lmhpoly.com/how-to-generate-the-environment-cubemap-in-unity/

  1. 编辑器扩展代码. 这是官方提供的脚本

    using System.Collections;
    using UnityEditor;
    using UnityEngine;
    
    public class RenderCubemapWizard : ScriptableWizard {
        public Transform renderFromPosition;
        public Cubemap cubemap;
    
        void OnWizardUpdate () {
            string helpString = "Select transform to render from and cubemap to render into";
            bool isValid = (renderFromPosition != null) && (cubemap != null);
        }
    
        void OnWizardCreate () {
            GameObject go = new GameObject ("CubemapCamera");
            go.AddComponent<Camera> ();
            go.transform.position = renderFromPosition.position;
            go.transform.rotation = Quaternion.identity;
            go.GetComponent<Camera> ().RenderToCubemap (cubemap);
            DestroyImmediate (go);
        }
    
        [MenuItem ("Tools/Render into Cubemap")]
        static void RenderCubemap () {
            ScriptableWizard.DisplayWizard<RenderCubemapWizard> (
                "Render cubemap", "Render!");
        }
    }
    
  2. 新建一个空的 cubemap, asset -> create -> legacy -> cubemap, 并设置参数. ( 若修改 cubemap 参数崩溃, 可能是版本问题, 参考: [使用 unity2018.2.8f1 版本, 修改 cubemap 的参数总是崩溃.](#使用 unity2018.2.8f1 版本, 修改 cubemap 的参数总是崩溃.) )

  3. 打开脚本工具, 设置 位置 和 新创建的 cubemap. 点解 render. done!

    • 在使用到场景中


踩坑

使用 unity2018.2.8f1 版本, 修改 cubemap 的参数总是崩溃.

解决办法: 我升级到 unity2018.3.11f1 就ok了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蝶泳奈何桥.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值