【Unity】基于高度图的体素世界生成与碰撞箱优化

该博客展示了如何使用Unity结合高度图生成体素地形,并进行了碰撞箱的优化。主要内容包括输入地形的初步设计,通过生成高度图来塑造地形,最终形成体素化的地形模型。博主提到了在实现过程中遇到的DepthCamera shader问题及其解决方案,提供了FullInspector和FullSerializer的相关链接作为辅助工具。
摘要由CSDN通过智能技术生成

成品

先放成品,原理与解释等以后补上

输入:地形大致样子

在这里插入图片描述

过程中:生成高度图

在这里插入图片描述

结果:生成体素地形

在这里插入图片描述

结果:优化后的碰撞箱

在这里插入图片描述

主要代码:

using FullInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;


[ExecuteInEditMode]
public class WorldBuilder : BaseBehavior<FullSerializerSerializer> {
   

    public GameObject sandBox;

    public DepthCamera depthCamera;
    public int groundHeight;
    public Texture2D texture;

    public Transform pointA;//drop
    public Transform pointB;//drop

    public Transform terrainParent;
    public Transform terrainColliderParent;

    public LayerMask mask;

    public int layer = 16;

    public List<GameObject> nowSelectTerrain;
    public List<GameObject> nowSelectSubTerrain;
    //public List<GameObject> nowSelectObj;

    //public bool enableDrawLine = false;
    //public KeyCode drawKey = KeyCode.B;
    //public float radius = 5;
    //public float density = 0.1f;
    //public AnimationCurve strength;

    int dertaFrame = 5;
    int frameCounter = 0;

    Vector3 textureSize;
    Vector3 terrainSize;
    Vector3 posMin, posMax;

    Camera sceneCamera;

    Vector3 sandBoxSize;
    Vector3 sandBoxOffset;

    //public Vector3 bufferedPosition = Vector3.zero;
    // Update is called once per frame
    void Update() {
   
        //if(enableDrawLine ) {
   
        //    if (CanUpdateLine()) {
   
        //        sceneCamera = SceneView.lastActiveSceneView.camera;
        //        var ray = sceneCamera.ScreenPointToRay(Input.mousePosition);
        //        RaycastHit hit;
        //        Physics.Raycast(ray,out hit,100, mask.value);
        //        bufferedPosition = hit.point;
        //        Debug.DrawRay(hit.point, Vector3.up * (posMax.y - posMin.y));
        //    }
        //}
    }

    bool CanUpdateLine() {
   
        frameCounter++;
        if (frameCounter >= dertaFrame) {
   
            frameCounter = 0;
            return true;
        } else return false;
    }

    [InspectorButton]
    public void Build() {
   
        Clean();
        UpdateParams();
        depthCamera.Snap(callback:CreateTerrain);
    }

    [InspectorButton]
    public void BuildWithCorrentSnap() {
   
        Clean();
        UpdateParams();
        CreateTerrain();
    }

    [InspectorButton]
    void Clean(string tag = "Terrain") {
   
        for(int i = terrainParent.childCount - 1; i >= 0; i--) {
   
            var obj = terrainParent.GetChild(i) ;
            if (obj.tag == tag) {
   
                obj.SetParent(transform);
                DestroyImmediate(obj.gameObject);
            }
        }
        for (int i = terrainColliderParent
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值