Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

July 15, 2013

Script, Environment Cubemap into Unity Skybox images


Continuing from my earlier post this year about rendering environment cubemaps in blender, for use in Unity, i recently had to do a volumetric cloud setup and render quite a few of these. Splitting them by hand, started to get a bit tedious, so i wrote this little script making it easier.

It will turn the image from blender,




into separated and properly named images like this:




This was put together really quick, so it is a bit rough, it will use the original filename and simply add the _position to it. However, it will not check if the file already exists (if it does, it will overwrite it without asking). I take no responsibility in any damage this might cause, use at your own risk, but maybe it is useful to someone else. Feel free to use, change and/or share this.

Here is the gist of it, it also requires the Python Imaging Library to be installed.


Usage
python split-skymap-into-images.py path/to/cubemap.png
The split images will be saved in the same directory as your cubemap.

February 12, 2013

ScreenMirror App

If you draw often, you probably know that your eyes tend to get used to what you look at pretty quick, and when you come back later, you suddenly find -obvious- mistakes in your work, that you did not notice at all while drawing.

A common help with this, is holding a drawing against a mirror - to get a fresh look on it - which usually makes mistakes pop out immediately. I've known people who keep a mirror right above their drawing board, so they have a mirrored view available all the time.

If you're working digital, flipping the image horizontally has the same effect.

A while back, i've had a talk with Nicc about Photoshop, not being able to give you a realtime mirrored preview when drawing. It does allow opening a secondary view of a document, but as soon as you flip one, the other will as well. I first was thinking of writing a Photoshop plugin to make this work, but eventually ended up making a small java application that allows recording an area of the screen and showing a flipped (or distorted) view of it. 






It's not very polished right now, but it should work fine.
Figured i post it, maybe someone else has a use for it, or is just curious.

A quick summary page for it (and downloads for mac/win/linux), can be found here.

I'd consider it an alpha build, so there might be bugs - use at your own risk -

August 04, 2012

Find out if a point is inside a volume.

Cleaned out the blog, after abandoning it for almost half a year (busy times).

I've been experimenting with procedural architecture generation lately, usually these systems follow a grid based layout, where you fill an array to calculate tiles.
However, i needed more precision for what i was trying to do, and the ability for angles and circular layouts, so i ended up trying a different route storing only edge points in space.
This lead to some problems, one was that i needed a way to quickly figure out if a point is inside a volume (to say, avoid placing a chest inside a wall).
Checking the collider bounds through physics wasn't an option, since this was needed to be done long before constructing actual geometry.

Telling quickly if a point is inside or outside a shape.


So here is the simple solution i ended up with, for anyone else who might be struggling with this.

It's based on the Point in polygon algorithm (also some more information on it here), which works by projecting a ray through the polygon counting the intersection points.
With the ray starting at 0, entering (first intersection) will turn it to 1, leaving to 2, entering again 3, leaving again 4... making it very easy to tell if the point is inside or outside, simply by checking if it results in an even or odd number of intersections.

    // this works on a flat plane, ignoring position in y
    bool PointInVolume(Vector3 point, Vector3[] arr)
    {
        var oddOrEven = false;         
        var x = point.x;
        var z = point.z;
        float pointCurrX, pointCurrZ, pointLastX, pointLastZ;

        var pointsInVolume = arr.Length;
        var j = pointsInVolume - 1;
           
        for (int i = 0; i < pointsInVolume; i++)
        {
            pointCurrX = arr[i].x;
            pointCurrZ = arr[i].z;
            pointLastX = arr[j].x;
            pointLastZ = arr[j].z;
                
            if ((pointCurrZ < z && pointLastZ >= z || pointLastZ < z && pointCurrZ >= z) && 
                (pointCurrX <= x || pointLastX <= x )) 
                if (pointCurrX + (z - pointCurrZ) / (pointLastZ - pointCurrZ) * (pointLastX - pointCurrX) < x) 
                    oddOrEven =! oddOrEven; 
                
            j = i; 
        }               
        return oddOrEven;
    } 



After the testing worked, i ended up adding a simple overload for calculating the volume around the point, so i could send in the size of the object as well, making sure it fits (keep in mind this works with a rectangular shape, for complex shapes it needs to be adjusted.)

    // looking for a volume at a centerpoint (point), width and height
    bool PointInVolume(Vector3 point, Vector3[] arr, float width, float height)
    {
        var hX = width / 2;
        var hY = height / 2;
        var widthMax = point.x + hX;
        var widthMin = point.x - hX;
        var heightMin = point.z + hY;
        var heightMax = point.z - hY;        
        var topR = new Vector3(widthMax, point.y, heightMax);
        var topL = new Vector3(widthMin, point.y, heightMax);
        var btmR = new Vector3(widthMax, point.y, heightMin);
        var btmL = new Vector3(widthMin, point.y, heightMin);

        if (PointInVolume(point, arr) && 
            PointInVolume(topR, arr) &&  
            PointInVolume(topL, arr) && 
            PointInVolume(btmR, arr) && 
            PointInVolume(btmL, arr))
            return true;
        else
            return false;
    }