I saw a few posts on this but couldn’t make much sense of them. I’m trying to make a class that provides easy procedural texture making for wood. The class is simply called Wood. The constructor sets all of the private variables and calls a method that does all of the texture generation. Thats where the problem comes in. I’ve created an instance of the class in another script so that I can test it out. However, I’m getting errors, even in the editor, before clicking play.
public static class Texture
{
[System.Serializable()]
public class Wood
{
//Public
public UnityEngine.Texture textureMap;
public UnityEngine.Texture normalMap;
//Private
public int width = 16;
public int height = 16;
public int seed = 1;
public float ringRadius = 1f;
public float mainFrequency = 1f;
public float mainFrequencyPower = 0.1f;
public float grainFrequency = 16f;
public int grainOctaves = 4;
public Vector3 grainScale = new Vector3(1f, 1f, 0.25f);
public Vector3 woodPosition = new Vector3(-3f, 1.5f, 1.5f);
public Vector3 woodRotation = new Vector3(80, 20, 45);
public Vector3 woodScale = new Vector3(16, 16, 16);
public UnityEngine.Texture2D ramp;
//Methods
public void Update()
{
//Rings of wood
var rings = new CoherentNoise.Generation.Patterns.Cylinders(ringRadius);
//The wood's grain
var grain = new CoherentNoise.Generation.Fractal.PinkNoise(seed);
grain.Frequency = grainFrequency;
grain.OctaveCount = grainOctaves;
var scaledGrain = grain.Scale(grainScale.x, grainScale.y, grainScale.z);
//Combination of the wood rings and grain
var wood = 0.25f * scaledGrain + rings + 0.1f;
//Add turbulence to the wood
var perturbedWood = wood.Turbulence(mainFrequency, mainFrequencyPower, seed);
//Grab a slice the wood
var slice = perturbedWood.Translate(woodPosition.x, woodPosition.y, woodPosition.z).Rotate(woodRotation.x, woodRotation.y, woodRotation.z).Scale(woodScale.x, woodScale.y, woodScale.z);
this.textureMap = TextureMaker.RampTexture(width, height, slice, ramp);
this.normalMap = TextureMaker.BumpMap(width, height, slice);
}
public void UpdateTexture()
{
//Rings of wood
var rings = new CoherentNoise.Generation.Patterns.Cylinders(ringRadius);
//The wood's grain
var grain = new CoherentNoise.Generation.Fractal.PinkNoise(seed);
grain.Frequency = grainFrequency;
grain.OctaveCount = grainOctaves;
var scaledGrain = grain.Scale(grainScale.x, grainScale.y, grainScale.z);
//Combination of the wood rings and grain
var wood = 0.25f * scaledGrain + rings + 0.1f;
//Add turbulence to the wood
var perturbedWood = wood.Turbulence(mainFrequency, mainFrequencyPower, seed);
//Grab a slice the wood
var slice = perturbedWood.Translate(woodPosition.x, woodPosition.y, woodPosition.z).Rotate(woodRotation.x, woodRotation.y, woodRotation.z).Scale(woodScale.x, woodScale.y, woodScale.z);
this.textureMap = TextureMaker.RampTexture(width, height, slice, ramp);
}
public void UpdateNormal()
{
//Rings of wood
var rings = new CoherentNoise.Generation.Patterns.Cylinders(ringRadius);
//The wood's grain
var grain = new CoherentNoise.Generation.Fractal.PinkNoise(seed);
grain.Frequency = grainFrequency;
grain.OctaveCount = grainOctaves;
var scaledGrain = grain.Scale(grainScale.x, grainScale.y, grainScale.z);
//Combination of the wood rings and grain
var wood = 0.25f * scaledGrain + rings + 0.1f;
//Add turbulence to the wood
var perturbedWood = wood.Turbulence(mainFrequency, mainFrequencyPower, seed);
//Grab a slice the wood
var slice = perturbedWood.Translate(woodPosition.x, woodPosition.y, woodPosition.z).Rotate(woodRotation.x, woodRotation.y, woodRotation.z).Scale(woodScale.x, woodScale.y, woodScale.z);
this.normalMap = TextureMaker.BumpMap(width, height, slice);
}
//Constructors
public Wood()
{
Update();
}
public Wood(int width, int height, Texture2D ramp, bool createNormal)
{
this.width = width;
this.height = height;
this.ramp = ramp;
if(createNormal)
Update();
else
UpdateTexture();
}
public Wood
(
int width,
int height,
int seed,
float ringRadius,
float mainFrequency,
float mainFrequencyPower,
float grainFrequency,
int grainOctaves,
Vector3 grainScale,
Vector3 woodPosition,
Vector3 woodRotation,
Vector3 woodScale,
Texture2D ramp,
bool createNormal
)
{
this.width = width;
this.height = height;
this.seed = seed;
this.ringRadius = ringRadius;
this.mainFrequency = mainFrequency;
this.mainFrequencyPower = mainFrequencyPower;
this.grainFrequency = grainFrequency;
this.grainOctaves = grainOctaves;
this.grainScale = grainScale;
this.woodPosition = woodPosition;
this.woodRotation = woodRotation;
this.woodScale = woodScale;
this.ramp = ramp;
if(createNormal)
Update();
else
UpdateTexture();
}
}
}
The error takes me to the line in the Update method:
this.textureMap = TextureMaker.RampTexture(width, height, slice, ramp);
It says:
NullReferenceException: Object reference not set to an instance of an object
CoherentNoise.Texturing.TextureMaker.RampTexture (Int32 width, Int32 height, CoherentNoise.Generator noise, UnityEngine.Texture2D ramp)
Procedural+Texture+Wood.Update () (at Assets/Code/Framework/Procedural.cs:71)
Procedural+Texture+Wood…ctor () (at Assets/Code/Framework/Procedural.cs:117)
So that this error isn’t returned, I tried putting the line(and the one below it) in an if statement
if(ramp != null)
{
this.textureMap = TextureMaker.RampTexture(width, height, slice, ramp);
this.normalMap = TextureMaker.BumpMap(width, height, slice);
}
However, when I add the if statement, I get two new errors (which point to the if statement):
CompareBaseObjectsInternal can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
ArgumentException: CompareBaseObjectsInternal can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.Object.CompareBaseObjects (UnityEngine.Object lhs, UnityEngine.Object rhs) (at C:/buildslave/unity/build/artifacts/EditorGenerated/UnityEngineObject.cs:49)
UnityEngine.Object.op_Inequality (UnityEngine.Object x, UnityEngine.Object y) (at C:/buildslave/unity/build/artifacts/EditorGenerated/UnityEngineObject.cs:162)
Procedural+Texture+Wood.Update () (at Assets/Code/Framework/Procedural.cs:71)
Procedural+Texture+Wood…ctor () (at Assets/Code/Framework/Procedural.cs:120)