splatmap textures not applying in build in unity - c#

im procedurally creating my heightmaps and declaring my terrain in the scripts
in the unity editor, my splatmaps work perfectly, however when i build my terrain is just plain black.
is there a setting that i may have missed when building that is required to make the splats work ?.
editor
http://answers.unity3d.com/storage/temp/31838-untitle2d.png
build
http://answers.unity3d.com/storage/temp/31839-untitled.png
i load them in using code with
SplatPrototype[] terrainTexture = new SplatPrototype[4];
terrainTexture[0] = new SplatPrototype();
terrainTexture[0].texture = (Texture2D)Resources.Load("stone");
terrainTexture[1] = new SplatPrototype();
terrainTexture[1].texture = (Texture2D)Resources.Load("sand");
terrainTexture[2] = new SplatPrototype();
terrainTexture[2].texture = (Texture2D)Resources.Load("grass");
terrainTexture[3] = new SplatPrototype();
terrainTexture[3].texture = (Texture2D)Resources.Load("snow");
edit
i attempted adding the textures to the code as variables draged in rather than using resource.load, also didnt work
edit
i found a work around which makes it work, but its unfortunate that i coudnt find a proper solution.
can anyone see something that could be the cause of the issue
http://
answers.unity3d.com/questions/171150/terrain-basemap-texture-through-code.html

You need to include an extra shader in your build. Go to edit -> project settings -> graphics. In the inspector increase the size. In the new slot select Nature/Terrain/Diffuse.
The reason you need to do this is that Unity will search for any assets that are referenced in the scene to decide what to include in the build and what to leave out. Since your terrain is built procedurally there is no terrain object and no terrain material present, and the required shader is not included. By following the above steps you basically tell Unity to include it anyway because you know you are going to need it.

I would add in a few things to the above answer:
There is a prototypes refresh, called TerrainData.RefreshPrototypes() that is supposed to refresh all available prototypes, including the detail textures and trees. Since the ground textures are part of the prototypes listing, this will help unity update the new textures for the terrain. (And, it also works inside of the Editor.)
Also, others have found that when they change the textures' data (ie, changing the pixel data) that it causes unity to update the materials used for the terrain. They made a blank terrain, assigned blank base textures to it, and then at runtime loaded up the height map, splat maps, and overwrote the textures. This caused unity to display the newly created terrains.
I hope that this helps out some, as most of the terrain asset is a highly undocumented area (and rather dysfunctional, too)... Good Luck!

Related

Manipulating 2D Lighting with editor version 2021.2.19f1 with C#

I'm not sure what I am doing wrong, but I am currently having an issue manipulating the 2D lighting of objects. Everything looks normal in visual studio, no errors, but when I try to initialize the code in Unity, it errors out and says the object doesn't have Light on it. 2d lighting seems to be a script, instead of a different type of object like with 3d lighting, but I can't reference the script. It's very hard to find info on this because it seems like the URP gets updated very regularly, and nothing I've found explains what I need to do to manipulate the settings like intensity.
Does anyone have any advice for me?
First create a new 2D light by going to Light < 2D < Point Light 2D (You can use any Light).
Now you should have a 2D light in your scene. Next attach a script to the light for example LightManipulator.cs.
Inside of LightManipulator.cs you can now reference the 2D light by declaring it as a public variable.
public Light2D light;
You can now change various values of this light. For example setting the intensity to 0.5f.
light.intensity = 0.5f;
EDIT: You have to import this at the top using UnityEngine.Experimental.Rendering.Universal;

How can I draw lines on the edges of objects?

I'm making a unity SteamVR game.
I want to achieve an unlit art style reminiscent of Antichamber.
I want to draw lines on the edges of objects, but I couldn't find an online solution that was not outdated and that worked.
Here is a picture from antichamber.
You can see that every object has a line across all of it's edges.
How can I achieve this effect?
Yo can do this with Unity's "Edge Detect Effect Normals" which is part of Unity's image effects and is included in their Standard Assets. You can get it here.
There is also a new image effect for Unity called "Post Processing Stack" which is really faster than the one I linked above and you can get that here. Edge Detect is not included in the new "Post Processing Stack" image effects but if you decide to use the new Post Processing Stack, you can get the ported version of "Edge Detect Effect Normals" here.
Any one of these effects can draw lines on edges of objects.
I want to draw lines on the edges of objects
This free asset promises to do just that.
"Supports outlines with variable thickness, color, that work in Editor, Ortho and Perspective cameras."
Source: https://assetstore.unity.com/packages/vfx/shaders/toon-shader-free-21288

Unity 2D - Dynamically instantiate new prefab inside NGUI's "UI Root"(as Parent)

Im new to unity. When I instantiate a new prefab GameObject from inside script as follows:
GameObject newArrow = (GameObject)Instantiate(arrowPrefab);
newArrow.transform.position = arrowSpawnTransform.position;
But this is creating the object in the root hierarchy(and not inside "UI Root" of NGUI). When I add any object outside of the UI-Root (of NGUI) it adds it to some location far away from the camera, also with a huge dimension. Can someone help me with how to add the newly created prefab under "UI Root" ?
It would be great of someone also lets me know about the positioning and scaling associated with native unity and NGUI. I try hard but am not understanding where to keep what object and in what size so that it comes out as expected. I'll appreciate any help that can be provided. Thanks !
EDIT:
I have found a way to place the new prefab inside "UI Root" thru:
newArrow.transform.parent = gameObject.transform.parent;
after instantiating.
But still the scaling is huge. It's like multiple times bigger than the screen size. Please help me with this. What should I be doing ?
When working with UI elements in NGUI, don't use Instantiate. Use NGUITools.AddChild(parent, prefab).
NGUI's scale with respect to the rest of the objects in the scene is rather microscopic. If you look at the UIRoot object, you will notice that its scale is measured in thousandths of a meter (default object resolution of 1 typically represents a meter). That is why when you instantiate a prefab and attach it to any object under the UIRoot it appears gigantic relative to the scale of the UIPanel. You could manually scale down the object to the appropriate size, or let NGUI do it for you (as indicated by Dover8) by utilizing NGUITools.AddChild(parent, prefab). In fact, this is the proper way to do so. Don't try to do it manually. The results will be unpredictable and can lead to inappropriate behavior of components. Here's a link to Tasharen's forum on this topic: http://www.tasharen.com/forum/index.php?topic=779.0
Positioning is a bit more complicated. Everything is relative to anchors. Anchor positions are a relationship between a parent object (usually a panel) and a target (usually some form of widget such as a sprite or label). They are controlled by four values relative to the edges of the panel (or parent object), right, left, bottom, and top with respect to the edges of the target (varies by position). Each of these are modified by an integer value (in pixels) that modify the dimensions of the target relative to the parent. There are many examples included with NGUI. I suggest that you look over them. In particular, pay attention to Example 1 - Anchors. I learned a lot from studying these examples, but they don't really cover the full power of NGUI, but they are an excellent starting point.

drawing a line following an object in unity c#

Hello I am using this code to draw a line that follows an object
Debug.DrawLine(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z),
new Vector3(prevPosition_x, prevPosition_y, prevPosition_z), Color.red,1.0f);
prevPosition_x = gameObject.transform.position.x;
prevPosition_y = gameObject.transform.position.y;
prevPosition_z = gameObject.transform.position.z;
However this doesnt draw in a build. Does anyone know how to do this without using the built in line or trail renderers. I know it can be done with gl but dont have a clue how to do this.
Any help is appreciated
I do believe Debug.DrawLine only works in the Scene Editor.
There are several ways you can draw lines, GL is definitely one of them. There is also Vectrosity.
Alas, you can do it yourself with some effort by generating your own line mesh and moving the vertices around.
Hope it helps.
As Xerosigma said, Debug.DrawLine only works in Editor, because it's purpose is to debug scripts, not provide visualization for users. Another problem is that this way, you'll only create lines that will render one frame, and previous to current position will only differ by one frame. You, on the other hand, probably want the trail not to be dependent on framerate.
I think that you'll find TrailRenderer component in Unity fitting for your situation. I've used it in one of my hobbie projects, and it works just fine.
I think that's not the best way, but should work
http://rockonflash.wordpress.com/2010/04/17/how-to-do-lasers-in-unity3d/
Debug.DrawLine only works on Scene Window

XNA - How to access a texture from .x model?

I am just getting into XNA programming and have been unable to figure out how can I access the texture from a ".x" model. I am using a custom shader to display my model (just a cube with a texture mapped on it) with the filters set to point. To do this I needed to pass the effect my texture file which needed to be imported separately from my model or else it would complain since it is included in my model as well. This works perfectly how I want it, however this isn't really an agreeable method when I have many different models with their own textures.
My question is:
How am I able to access the texture included in my model directly from it and send that to my shader? Or am I able to access it directly with HLSL?
What I have tried:
I have found posts saying that it can assigned to a texture variable with:
Texture2d texture = ((BasicEffect)model.Meshes[0].Effects[0]).Texture
When I tried this the game runs but the cubes are just black. I can see that the texture variable is holding info and has the right dimensions but I can't tell if it is correctly holding the actual image. When I used just the BasicEffect they rendered just fine with their texture.
Update:
I have managed to get this to work after a little bit of fiddling. My game loads in a few hundred of the same cube and upon creation of each it would try save the texture of the model using the code above and then go through the mesh parts and change the effects to my custom effect. I discovered that the first cube created would save the texture okay but any subsequent cubes created would complain that they can't be cast as a BasicEffect. This resulted in one textured cube and then a lot of black ones. I am guessing that when it reuses the same model over and over like that it will just use the one that was modified to use my custom effect which was done on the first instance of the cube. Is this normal? I have got them all to render as textured by changing the texture variable to static.
Please observe that you are assigning the texture of your model to a temporary Texture2D variable, and not setting the Texture present in the Effect currently tied to your mesh.
If you do the following:
Texture2D textureToSet = Content.Load<Texture2D>("MyTex");
//Keep in mind that this method requires a basic effect type and that only one
//effect is present on each mesh to work properly.
foreach(Mesh mesh in model.Meshes)
{
((BasicEffect)(mesh.Effects[0])).Texture = textureToSet;
}
The quirky stuff going on inside the foreach is simply that you are grabbing the effect, then casting it to a BasicEffect and using its Texture property to give it a new texture to draw when used. Please see the documentation and Shawn's blog for a more detailed introduction.
If anyone else is wondering about this as I was then saving the texture using:
Texture2d texture = ((BasicEffect)model.Meshes[0].Effects[0]).Texture
This does work but there is one thing to watch for which is what was causing me problems. If you change the effect of the model from the default BasicEffect for one instance it will be changed for every instance of the model created thereafter. So you will only be able to use the above code before you change the effect for the first time on a particular model.
I later found this book which describes exactly how to extract the texture and other information from a model: 3D Graphics with XNA Game Studio 4.0 by Sean James - Specifically chapter 2

Categories

Resources