# Unity Ray Pixel collision

## Recommended Posts

Hi guys

Ive been working on a project involving  ray  collision  with a multitextured terrain(2D heightmap)  the ray method(below) not only returns the triangle intersected but the uv(barycentric) coordinates as well but during research for my project I came across  a post from the unity website(link below) that had to do with  ray pixel intersection and wondered if I should implement that instead, so the question that Im asking is which of the two is better. should I stick with the ray uv coordinate method or go with an XNA version of the ray pixel collision.

Thankyou

unity ray pixel post

the intersect method Ive been working with

public   bool RayTriangleIntersect(Vector3 ray_origin, Vector3 ray_direction,
Vector3 vert0, Vector3 vert1, Vector3 vert2,
out float? t, out float u, out float v)
{
t = 0; u = 0;  v = 0;

Vector3 edge1 = vert1 - vert0;
Vector3 edge2 = vert2 - vert0;

Vector3 tvec, pvec, qvec;
float det, inv_det;

pvec = Vector3.Cross(ray_direction, edge2);

det = Vector3.Dot(edge1, pvec);

if (det > -0.00001f)
return false;

inv_det = 1.0f / det;

tvec = ray_origin - vert0;

u = Vector3.Dot(tvec, pvec) * inv_det;
if (u < -0.001f || u > 1.001f)
return false;

qvec = Vector3.Cross(tvec, edge1);

v = Vector3.Dot(ray_direction, qvec) * inv_det;
if (v < -0.001f || u + v > 1.001f)
return false;

t = Vector3.Dot(edge2, qvec) * inv_det;

if (t <= 0)
return false;

return true;
}


also a youtube link to a project called rigs of rods to give a visual of the effect Im working on,

which is to have  a vehicle shoot out certain particles(dirt debris,grass debris,etc,..) based on

the texture its riding over.

##### Share on other sites

yet another option:

the pickray example in the directx sdk's.

its probably the one that XNA's is based off of.

is it necessary to cast a ray?  there's no other way to know what texture is at location x,z  (the vehicle's location)? i take it the ground mesh is just a big textured mesh, with no underlying map, and you only have the artwork geometry to work with?

##### Share on other sites

i take it the ground mesh is just a big textured mesh, with no underlying map, and you only have the artwork geometry to work with?

Thanks for responding Norman,what would be the best type of terrain for this effect, right now Im using the shader based drawn terrain from riemers( http://www.riemers.net/eng/Tutorials/XNA/Csharp/series4.php)but would another approach do the job, like for example texture mapping(https://en.wikipedia.org/wiki/Texture_mapping),and what type of map do you think was used in this other youtube link below.

ultimate ninja storm

Thanks again

##### Share on other sites

right now Im using the shader based drawn terrain from riemers( http://www.riemers.net/eng/Tutorials/XNA/Csharp/series4.php)

a quick look at that link reveals that they are procedurally creating a large ground mesh, using a bitmap as the heightmap. and they are using the height at a given location to determine the blending weights of 4 textures they use, blending at most two together at once.

you would use a function similar to the one that maps height to blending weights, to map height to the closest single texture (the texture with the highest weight). that texture would determine the particle type to be emitted (mud, dust, gravel, etc). in border cases where the weights were equal, you'd choose some convention such as always going with the texture used for the "higher" altitude.

a function that maps from location to height to texture to particle type would get you just what you want: a function that maps from location to particle type based on underlying texture.

your heightmap function already does the location to height mapping.

you already know what texture you want to map to what kind of particle.

all you have to do is mod a copy of "height to blend weights" to create your "height to texture" function.

what would be the best type of terrain for this effect,

i don't quite follow....

do you mean the best type of terrain data structure?

in your case, the data structure is a bitmap heightmap, from which a ground mesh is procedurally generated, and from which texture blend weights are procedurally generated (on the fly apparently).

the downside of texture blend weights procedurally generated from a bitmap height map (on the fly) is that you have no "map" where you can just look up the texture at x,z and immediately create the appropriate type of particle emitter.. you must calculate the texture at x,z (on the fly) every time.   the workaround (if needed) is to implement an underlying map that you generate when you generate the mesh. in the underlying map, you calculate (one time only) and store the texture (and thus particle type emitted) at each location.

an underlying map might take the form of a 2d array of bytes, with values of 0 through 3 indicating which texture is dominant (heaviest blend weight) and therefore which particle type to emit. the look-up function would do something like take x,z and convert them to ints, then used those int values as the array[x][z] indices to return a texture/particle type ID number 0 through 3. note that such a look-up table/map could get large. a 100x100 mesh would use an underlying map of 100x100 = 10,000 bytes.

a 1000x1000 mesh would be 1 million bytes. and a 10,000x10,000 mesh would be 100 million bytes for the underlying map.

i personally use a "pattern map" to determine what ground texture goes where. a pattern map is just a small underlying map as described above. the difference is that a pattern map is only two times maximum visual range in size, instead of the size of the entire ground mesh, and gets repeatedly tiled across the ground mesh. the size is chosen to be as small as possible (to save ram), while being big enough that no repeating patterns due to tiling are ever visible from the camera's location while looking in all directions. my ground quads are 10x10, and my ground texture pattern map is 800x800. so the ground texture tile ID pattern map covers 80x80 quads, and only requires an 80x80= 6400 byte array. i use a single ground texture tile ID pattern map for all terrain types. i use 4 other pattern maps to place things like rocks, trees, berry bushes, fruit trees, and rock outcroppings. values in the ground texture pattern map are a simple ground texture tile set ID number 0 through 3. there are seamless ground texture tile sets (currently 4 tiles per set) for each terrain type. terrain type from the world map tells me the ground texture tile set used. the pattern map tells me which ground texture in the tile set is used at a given x,z location.

a pattern map lets you use seamless ground texture tile sets to reduce moire' patterns which you can get with just a single seamless ground texture tile (as seen in oblivion with distant ground textures).

texture blending / splatting is another way to reduce or eliminate moire' patterns.

would another approach do the job, like for example texture mapping(https://en.wikipedia.org/wiki/Texture_mapping),

weighted texture blending (what you're doing) is already an advanced form of texture mapping. plain old texture mapping would just draw a single texture, not do a weighted blending of two textures.

what type of map do you think was used in this other youtube link below.

unfortunately, i'm on a 3g cell modem out in the boonies with marginal signal (1xrtt protocol usually - think dialup + intermittent signal loss), so streaming video isn't really happening for me.   : (

it drives me crazy, as i've become addicted to Korean historical dramas like "Faith" and "Iron Empress" on hulu.com.    <g>

Edited by Norman Barrows

##### Share on other sites

THANKS A BUNCH NORMAN !!!

I was just about to give up on the heightmap idea and before reading your response  I  wrote a post about using a textured quad for a terrain instead(below), sorry about that, didnt want to come across as ungrateful for all of the help youve given me,but I will be putting the info you gave me to good use.

Thanks again

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628395
• Total Posts
2982433
• ### Similar Content

I'd like to start building a 2D game, very small, just pixel art. I know python/javascript/tiny bit of HTML and I've started learning C++. I'm a long way from starting officially, I've just been making mini-projects to practice & drawing up concept art/gameplay storyboards. I'm just wondering if I'm off with the C++ for the game's programming, or if I should know certain things before starting.
The game would be very small, I just want to get started trying out some actual work before college.
Some other info: I'm working by myself on this, and going into game design/programming next year for college. I'm a high school student with a few years of experience just working by myself making small projects, drawing up stuff, so I really don't know much and I'd appreciate any tips.
Thanks a bunch!
• By Daerst

SWARMED is a Zombie-themed RPG / RTS currently in development using Unity 3D. We love Dwarf Fortress (though we have no illusions that SWARMED will reach the same level of complexity), roguelikes, old-school point & click RPGs and real-time strategy games. We aim to cross genre-borders here and there and give some twists to the old Martinis every gamer has been drinking since the 1980s, metaphorically.
Single player, 3D graphics and adjustable top-down camera - old-school RPG / RTS feeling Take control of a core group of survivors after the outbreak Encounter Zombies that are a real threat, no machine-gun massacre. Don't get swarmed! Build a safe zone anywhere with a highly flexible build system: campsite, lighthouse, school, or fence a whole village Grant asylum to other survivors that you meet and make them a part of your community Achieve sustainability in your safe zone and go on supply runs with your survivors
Development
The core team of recently founded indie studio Three Eyed Games currently consists of one writer, two artists and two programmers, based in Germany. We are in our mid-20s with professional experience in developing interactive 3D applications with Unity.
SWARMED will feature both a 'free-play mode' and a campaign with mid-sized maps that leads the player through a story while explaining the gameplay and introducing him / her to the survivors: a core group a few 'hero' characters the player starts with (each one a detailed character with backstory, hopes and dreams), and more 'heroes' (total not more than 20, probably less) that the player can meet on the journey. In addition, randomly generated NPCs (less detailed and not directly controllable, similar to the way Dwarf Fortress handles its dwarves) can join your safe zone – if you let them.
We plan to release a few 'Origin' prototypes that showcase individual gameplay systems and meanwhile give a gentle introduction to the characters you will meet in the game. Origin I, showcasing the build system and many fundamental elements like character controls and interactions, is finished and will be released soon. Next up, we're working on the dialog system to be presented in Origin II. Get in touch and we will provide more details and a playable version.
We want you!
We seriously think you should join the fun! We are looking for:
Level Designers / Environment Artists, preferably with experience in Unity and procedural asset creation. Design and build maps with interesting visuals and proper pacing. 3D Artists. Our shacks, items and the dead guys' faces could use some plastic surgery. Can you do that? Writers. We have a bunch of characters to detail and a story to write ahead of us. Game Designers. We have a rough game design sketched out that needs improvement and completion. We need a balanced combat system, trees for constructions, workshops and character skills etc. PR & Community Managers, preferably with web development experience. We want to build a community around the game, and we need you to plan and manage this (with the help of the rest of the team, of course). 2D Artists / UX Designers, preferably with Unity UI experience. Our menus still look pretty dull, and we don't like that. We also need concept art for characters and iconic game moments to define their look and feel. Coders. If you know your way around Unity and C#, there are lots of challenging things to be done. You will work closely together with the two programmers already on the team to get going quickly. Please drop me a message or contact info@three-eyed-games.com

• itSeez3D, a leading developer of mobile 3d scanning software, announced today a new SDK for its automatic 3D avatar generation technology, Avatar SDK for Unity. The Avatar SDK for Unity is a robust plug-n-play toolset which enables developers and creatives to integrate realistic user-generated 3D avatars into their Unity-based applications. SDK users can allow players to create their own avatars in the application or integrate the SDK into their own production processes for character design and animation.
“Virtual avatars have recently become increasingly popular, especially in sports games and social VR apps. With the advance of VR and AR, the demand to get humans into the digital world is only increasing”, said Victor Erukhimov, itSeez3D CEO. “Our new Avatar SDK for Unity makes it super-easy to bring the avatar technology into any Unity-based game or VR/AR experience. With the Avatar SDK for Unity now every developer can bring face scanning technology into their games and allow players to create their own personalized in-game avatars, making the gameplay much more exciting and immersive.”
Key features of the Avatar SDK for Unity:
Automatic generation of a color 3D face model from a single selfie photo in 5-10 seconds (!). Works best with selfies, but can be used with any portrait photo.
Shape and texture of the head model are unique for each person, synthesized with a deep learning algorithm crafted by computer vision experts
Head models support runtime blendshape facial animations (45 different expressions)
Generated 3D heads include eyes, mouth, and teeth
Algorithms synthesize 3D meshes in mid-poly resolution, ~12k vertices, and ~24k triangles
Six predefined hairstyles with hair-recoloring feature (many more available on request)
Avatar generation API can be used in design-time and in run-time, which means you can allow users to create their own avatars in your game
Cloud version is cross-platform, and offline version currently works on PCs with 64-bit Windows (support for more platforms is coming soon)
Well-documented samples showcasing the functionality.

Availability
The Avatar SDK for Unity is offered in two modes - “Cloud” and “Offline”. The “Cloud” version is available at http://avatarsdk.com/ and the “Offline” version is available by request at support@itseez3d.com.
###
At itSeez3D, we are working on the computer vision technology that turns mobile devices into powerful 3D scanners. itSeez3D has developed the world's first mobile 3D scanning application that allows to create high-resolution photorealistic 3D models of people's' faces, bodies and objects. The application is available for iOS and Windows OS mobile devices powered with 3D cameras. In 2016 the company introduced Avatar SDK that creates a realistic 3D model of a face from a single selfie photo. To learn more about itSeez3D scanning software and 3D avatar creation technology, please visit www.itseez3d.com and www.avatarsdk.com.

View full story

• itSeez3D, a leading developer of mobile 3d scanning software, announced today a new SDK for its automatic 3D avatar generation technology, Avatar SDK for Unity. The Avatar SDK for Unity is a robust plug-n-play toolset which enables developers and creatives to integrate realistic user-generated 3D avatars into their Unity-based applications. SDK users can allow players to create their own avatars in the application or integrate the SDK into their own production processes for character design and animation.
“Virtual avatars have recently become increasingly popular, especially in sports games and social VR apps. With the advance of VR and AR, the demand to get humans into the digital world is only increasing”, said Victor Erukhimov, itSeez3D CEO. “Our new Avatar SDK for Unity makes it super-easy to bring the avatar technology into any Unity-based game or VR/AR experience. With the Avatar SDK for Unity now every developer can bring face scanning technology into their games and allow players to create their own personalized in-game avatars, making the gameplay much more exciting and immersive.”
Key features of the Avatar SDK for Unity:
Automatic generation of a color 3D face model from a single selfie photo in 5-10 seconds (!). Works best with selfies, but can be used with any portrait photo.
Shape and texture of the head model are unique for each person, synthesized with a deep learning algorithm crafted by computer vision experts
Head models support runtime blendshape facial animations (45 different expressions)
Generated 3D heads include eyes, mouth, and teeth
Algorithms synthesize 3D meshes in mid-poly resolution, ~12k vertices, and ~24k triangles
Six predefined hairstyles with hair-recoloring feature (many more available on request)
Avatar generation API can be used in design-time and in run-time, which means you can allow users to create their own avatars in your game
Cloud version is cross-platform, and offline version currently works on PCs with 64-bit Windows (support for more platforms is coming soon)
Well-documented samples showcasing the functionality.

Availability
The Avatar SDK for Unity is offered in two modes - “Cloud” and “Offline”. The “Cloud” version is available at http://avatarsdk.com/ and the “Offline” version is available by request at support@itseez3d.com.
###
At itSeez3D, we are working on the computer vision technology that turns mobile devices into powerful 3D scanners. itSeez3D has developed the world's first mobile 3D scanning application that allows to create high-resolution photorealistic 3D models of people's' faces, bodies and objects. The application is available for iOS and Windows OS mobile devices powered with 3D cameras. In 2016 the company introduced Avatar SDK that creates a realistic 3D model of a face from a single selfie photo. To learn more about itSeez3D scanning software and 3D avatar creation technology, please visit www.itseez3d.com and www.avatarsdk.com.

• I'm looking for programmers for an rpg I want to make. If you're wondering what "semi-turn based" means, it means that you take turns, but instead of a rigid back and forth like Pokemon, a timer determines when you can act, a sort of modernization of the classic Final Fantasy Active Time Battle system. Right now, I'm looking for programmers to create a prototype of both the combat system and the movement outside of combat. Preferably for Unity C#. Concept artists, particularly for characters, and writers to help me flesh out the character and story aspects, would also be helpful.
Here's a concept doc to fully explain things: https://docs.google.com/document/d/1ObDMAUWsndSAJ1EpQGRDxtR8Xl9xPotx89OZ0sgRaIw/edit?usp=sharing
If you can fill another role and are interested, feel free to let me know as well.
At the moment, this is purely a hobby project, with no payment planned. If we produce something we feel we can release, then of course we'll work out something for compensation. But, again, don't join this project counting on payment.
If you're interested, contact me on here, or at jordestoj@yahoo.com . Thanks.

• 10
• 9
• 19
• 24
• 10